数据分析 Matplotlib 之饼图
饼图基本使用
除了条状图, 饼图也可以用来表示数据.用pie()函数制作饼图很简单.
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 设置图像大小
plt.figure(figsize=(16,9))
# 设置标签
labels = ['China', 'Japan', 'Korea', 'American', 'Russian']
# 标签对应的值
values = [60, 10, 20, 70, 50]
# 每一个标签饼图的颜色
colors = ['red', '#FEDD62', 'blue', 'gray', 'green']
# 那一块内容需要脱离饼图凸显, 可选值0-1
explode = [0.1, 0, 0, 0, 0]
# autopct='%1.1f%%'表示显示百分比
# shadow显示阴影
# startangle 正值表示逆时针旋转
plt.pie(values,
labels=labels,
colors=colors,
explode=explode,
startangle=90,
shadow=True,
autopct='%1.1f%%')
# 设置为标准圆形
# plt.axis('equal')
# 显示图例
plt.legend(loc=2)
plt.show()

饼图案例
数据截图:

数据来源: http://data.stats.gov.cn/easyquery.htm?cn=C01 数据共7行10列. 2006年-2015年期间国内各项税收收入.
完成任务:
- 计算并显示各项税收所占税收比例,并通过饼图展现.
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 加载字体
font = FontProperties(fname='/Library/Fonts/Songti.ttc', size=18)
# 年份
year = '2015年'
# 加载数据
tax_data = pd.read_csv('国家各项税收.csv',
index_col=0,
skiprows=2,
skipfooter=1,
engine='python')
print(tax_data.iloc[:, :5])

# 计算其他税收 other_tax = tax_data[year].iloc[0] - tax_data[year].iloc[1:].sum() other_tax

ret = tax_data[year] ret['其他税收'] = other_tax ret

# 设置图像大小
plt.figure(figsize=(16, 9))
font = FontProperties(fname='/Library/Fonts/Songti.ttc', size=18)
font2 = FontProperties(fname='/Library/Fonts/Songti.ttc', size=12)
# 将个人所得税分离显示
explode = [0.2 if taxname == '个人所得税(亿元)' else 0 for taxname in ret.index]
my_pie = plt.pie(ret.values,
labels=ret.index,
shadow=True,
autopct='%1.1f%%',
explode=explode,
startangle=30)
# 设置饼图显示字体为中文
for txt in my_pie[1]:
txt.set_fontproperties(font)
txt.set_fontsize(14)
plt.show()

绘制多个图像
在matplotlib中, 一张图像是一个Figure对象. 在Figure对象中可以包含一个或者多个Axes对象。每个Axes(ax)对象都是一个拥有自己坐标系统的绘图区域。

我们可以通过subplot()函数在一个Figure上创建多个图像(Axes).
from pandas import DataFrame, Series
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 设置Figure大小
plt.figure(figsize=(15, 9))
# 增加一个Axes, 并选中当前Axes,在这里设置了2行3列Axes,选中的是第一个图形
plt.subplot(2, 3, 1)
plt.plot([0, 1, 2, 3, 4, 5], [0, 2, 3, 4, 5, 6], 'ro')
plt.title('this is my picture1')
plt.grid()
plt.legend(['First plot'])
plt.subplot(2, 3, 2)
plt.plot([0, 1, 2, 5, 4, 5], [0, 2, 3, 4, 5, 6])
plt.title('this is my picture2')
plt.subplot(2, 3, 3)
plt.plot([0, 1, 2, 5, 4, 5], [0, 2, 3, 4, 5, 6])
plt.title('this is my picture3')
plt.subplot(2, 3, 4)
plt.plot([0, 1, 2, 5, 4, 5], [0, 2, 3, 4, 5, 6])
plt.title('this is my picture4')
plt.subplot(2, 3, 5)
plt.plot([0, 1, 2, 5, 4, 5], [0, 2, 3, 4, 5, 6])
plt.title('this is my picture5')
plt.subplot(2, 3, 6)
plt.plot([0, 1, 2, 5, 4, 5], [0, 2, 3, 4, 5, 6])
plt.title('this is my picture6')
plt.show()

绘制曲线
from pandas import DataFrame, Series
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.plot(np.arange(-8, 8, 2), np.random.randint(-4, 4, 8))
# 设置刻度数值范围
plt.axis([-10, 10, -10, 10])
# 获得Axes对象
ax = plt.gca()
# 把坐标轴右边和上边的坐标轴的颜色设置为 none ,即隐藏
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
# ax.spines['left'].set_color('none')
# ax.spines['bottom'].set_color('gray')
# 把x轴放到最上面
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# 移动x和y轴到中心位置
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))
plt.show()


浙公网安备 33010602011771号