python matplotlib在一张图上画多个饼图!
数据展示
数据来源可参考我的这篇博客
https://blog.csdn.net/qq_45058254/article/details/104195171
python代码
参考https://blog.csdn.net/zengbowengood/article/details/103279583这篇博客来写的画图部分
#encoding=utf-8
#@Project filename:PythonDemo PieChart
#@IDE :PyCharm
#@Author :ganxiang
#@Date :2020/02/07 0007 12:41
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('./data/1月36CityConsumption.csv',encoding='gb18030')
#解决汉字乱码问题
plt.rcParams['font.sans-serif']=['SimHei'] #使用指定的汉字字体类型(此处为黑体)
fig,axj=plt.subplots(nrows=2,ncols=2,figsize=(8, 8),dpi=200) #建立饼图坑
axes = axj.flatten() #子图展平
for ax in range(0,4):
city = df['地区'][ax+1:ax+6]#控制读取['地区']列的变化
consumption =df['居民消费价格指数当月(上年同月=100)'][ax+1:ax+6]
col_sum = consumption.sum()
share = []
for j in consumption:#计算均值
share.append(round(j/col_sum,4))
explode = [0.1,0, 0, 0, 0]
explode[ax]=0.06
axes[ax].pie(x=share,labels=city,explode=explode,autopct = '%3.1f%%')
axes[ax].set_title("第"+str(ax+1)+"张图")
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.5, hspace=0.2) # 调整子图间距
plt.savefig('./data/居民消费价格指数.jpg')
plt.show()
结果展示