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

Python数据分析---matplotlib可视化(堆叠柱状图-月份)

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

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

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

堆叠柱状图

代码如下:

代码语言:python
复制
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

from matplotlib.patches import Patch
from matplotlib.lines import Line2D



plt.rcParams['font.family']='sans-serif'
plt.rcParams['font.sans-serif']='SimHei'
df=pd.read_excel('d:/网络收集数据.xlsx')
df2=pd.read_excel('d:/网络收集数据.xlsx',1)

width = 0.35       # the width of the bars: can also be len(x) sequence

df2.set_index('城市名',inplace=True)


citys=['郑州市', '开封市', '鹤壁市', '新乡市', '焦作市', '濮阳市','安阳市' ]
def getPlot(city):
    df=df2.loc[city]
    df.reset_index(inplace=True)
    fig, ax = plt.subplots(figsize=(10,6))
    labels=df.月份.map(lambda x:str(x)+'月')
    y1=df['2018年二氧化硫_省辖市'].apply(round) 
    y2=df['2018年氮氧化物_省辖市'].apply(round) 
    y3=df['2018年烟尘_省辖市'].apply(round) 


    y21=df['2019年二氧化硫_省辖市'].apply(round) 
    y22=df['2019年氮氧化物_省辖市'].apply(round) 
    y23=df['2019年烟尘_省辖市'].apply(round) 

    x = np.arange(len(labels))  # the label locations
    rects1=ax.bar(x-width/2.0, y1, width,  label='二氧化硫',color="tab:blue",linewidth=18)
    rects2=ax.bar(x-width/2.0,y2 , width,  bottom=y1,label='氮氧化物',color="tab:orange")

    rects3=ax.bar(x-width/2.0,y3 , width, bottom=y2+y1,label='烟尘',color="tab:green")


    rects11=ax.bar(x+width/2.0+0.04, y21, width,  label='二氧化硫',color="tab:blue")
    rects22=ax.bar(x+width/2.0+0.04,y22 , width,  bottom=y21,label='氮氧化物',color="tab:orange")

    rects33=ax.bar(x+width/2.0+0.04,y23 , width, bottom=y22+y21, label='烟尘',color="tab:green")


    ax.set_ylabel('吨')
 
    title='2018-2019年%s排放情况'%(city)
    ax.set_title(title)
    plt.xticks(x,labels)

    yy=np.array([y1,y2,y3])
    yy2=yy.cumsum(0)


    yyy=np.array([y21,y22,y23])
    yyy2=yyy.cumsum(0)
    def autolabel1(rects,rectIndex):
        """Attach a text label above each bar in *rects*, displaying its height."""
        for i,rect in enumerate(rects):
            height = (yy2[rectIndex-1][i] if rectIndex >0 else 0)+yy[rectIndex][i]/2.0
            value=yy[rectIndex][i]
            ax.annotate('{}'.format(value),
                        xy=(rect.get_x() + rect.get_width() / 2, height-5),
                        xytext=(0, 3),  # 3 points vertical offset
                        textcoords="offset points",
                        ha='center', va='bottom')


    def autolabel2(rects,rectIndex):
        """Attach a text label above each bar in *rects*, displaying its height."""
        for i,rect in enumerate(rects):
            height = (yyy2[rectIndex-1][i] if rectIndex >0 else 0)+yyy[rectIndex][i]/2.0
            value=yyy[rectIndex][i]
            ax.annotate('{}'.format(value),
                        xy=(rect.get_x() + rect.get_width() / 2, height-5),
                        xytext=(0, 3),  # 3 points vertical offset
                        textcoords="offset points",
                        ha='center', va='bottom')


    autolabel1(rects1,0)
    autolabel1(rects2,1)
    autolabel1(rects3,2)

    autolabel2(rects11,0)
    autolabel2(rects22,1)
    autolabel2(rects33,2)



 
    #ax.legend([rects1,rects2,rects3],['二氧化硫', '氮氧化物', '烟尘'])
    #ax2.legend([p1,p2],['2018年SO2监测值','2019年SO2监测值'])


    legend_elements = [

    Patch(facecolor='tab:blue', edgecolor='b',label='二氧化硫'),
    Patch(facecolor='tab:orange', edgecolor='b',label='氮氧化物'),
    Patch(facecolor='tab:green', edgecolor='b',label='烟尘'),
   
                   
                        ]

    ax.legend(handles=legend_elements, loc='best')
                     
    plt.savefig(title+'.png')

for city in citys:
    print(city)
    getPlot(city)
    

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

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

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

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

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