前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python数据分析---matplotlib可视化(饼状图)

Python数据分析---matplotlib可视化(饼状图)

原创
作者头像
MiaoGIS
修改2020-03-11 15:01:34
1K0
修改2020-03-11 15:01:34
举报
文章被收录于专栏:Python in AI-IOTPython in AI-IOT

偶然看到网上国家统计数据,利用Python数据分析自己做了几种图表练习。主要采用Pandas来做数据统计,matplotlib来做图表可视化。

下面图表数据来源于网络。

饼状图

代码如下:

代码语言:python
复制
import matplotlib.pyplot as plt
import pandas as pd
import itertools
plt.rcParams['font.family']='sans-serif'
plt.rcParams['font.sans-serif']='SimHei'
df=pd.read_excel('d:/2018-2019年空气质量均值.xlsx')
df2=pd.read_excel('d:/2018-2019年空气质量均值.xlsx',1)



# Pie chart, where the slices will be ordered and plotted counter-clockwise:
colNames=["二氧化硫","氮氧化物","烟尘","排放总量"]
years=["2018","2019"]
regionTypes=["省辖市","市辖区"]
def getPlot(colName,year,regionType):
    df22=df2
    if regionType=="市辖区":
        df22=df2[df2['城市名']!='济源市']
    g=df22.groupby(df22['城市名']).sum()
    colStr='%s年%s_%s'%(year,colName,regionType)
    explode = g[colStr].map(lambda x:0.1)  # only "explode" the 2nd slice (i.e. 'Hogs')

    
 

    sizes=g[colStr].apply(round)
    labels=g.index
    labels=sizes.reset_index().to_dict('records')
    labels=map(lambda x:'%s(%dt)'%(x['城市名'],x[colStr]),labels)
    labels=list(labels)
    fig1, ax1 = plt.subplots(figsize=(8,8))
    ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
        shadow=True, startangle=90)
    ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
    title="%s年各地市%s排放量分布(%s)"%(year,colName,regionType)
    plt.title(title)
    plt.savefig(title+'.png')

for colName,year,regionType in itertools.product(colNames,years,regionTypes):


    print(colName,year,regionType)
    getPlot(colName,year,regionType)

    


    

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 饼状图
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档