matplotlib:饼图、环形图、爆炸式饼图
和折线图类似,通用部分可查看折线图
1 饼图
饼图主要是看占比,最好是2-6类
import matplotlib.pyplot as plt
from matplotlib import rcParams
# Windows自带的黑体
rcParams['font.family'] = 'SimHei'
# 创建图表,设置大小
plt.figure(figsize = (10,5)) things = ['学习','娱乐','运动','睡觉','其他']times = [6,4,1,8,5]plt.pie(times,labels = things)
# 显示图表
plt.show()
添加标题
plt.title("一天的时间分布",color = 'red',fontsize = 20)
显示百分比占比
plt.pie(times,labels = things,autopct = '%1.1f%%')
设置颜色
colors = ["#66b3ff","#99ff99","#ffcc99","#ff9999","#ff4499"]plt.pie(times,labels = things,autopct = '%1.1f%%',colors = colors)
2 环形图
在上面饼图的基础上添加wedgeprops
即可
plt.pie(times,labels = things,autopct = '%1.1f%%',colors = colors,wedgeprops = {'width' : 0.6})
设置百分比数字到圆心的距离
plt.pie(times,labels = things,autopct = '%1.1f%%',colors = colors,wedgeprops = {'width' : 0.6},pctdistance = 0.7)
3 爆炸式饼图
在饼图的基础上修改
import matplotlib.pyplot as plt
from matplotlib import rcParams
# Windows自带的黑体
rcParams['font.family'] = 'SimHei'
# 创建图表,设置大小
plt.figure(figsize = (10,5)) things = ['学习','娱乐','运动','睡觉','其他']times = [6,4,1,8,5]colors = ["#66b3ff","#99ff99","#ffcc99","#ff9999","#ff4499"]plt.pie(times,labels = things,autopct = '%1.1f%%',colors = colors)plt.title("一天的时间分布",color = 'red',fontsize = 20)# 显示图表
plt.show()
设置突出块的位置
explode = [0.2,0,0,0,0]
plt.pie(times,labels = things,autopct = '&.1f%%',colors = colors,explode = explode)
设置阴影
plt.pie(times,labels = things,autopct = '&.1f%%',colors = colors,explode = explode,shadow = True)