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

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

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

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

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

柱状图

代码如下:

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

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

colNames=["PM25","PM10","SO2","NO2","优良天数"]
 

labels=df.月份
def getPlot(colName):
    
    dx =df['%s_2018年'%colName].apply(round)
    dy = df['%s_2019年'%colName].apply(round)
    x = np.arange(len(labels))  # the label locations
    width = 0.35  # the width of the bars

    fig, ax = plt.subplots()
    rects1 = ax.bar(x - width/2, dx, width, label='2018')
    rects2 = ax.bar(x + width/2, dy, width, label='2019')

    # Add some text for labels, title and custom x-axis tick labels, etc.
    ylabel='天' if colName=="优良天数" else "微克/立方米"
    ax.set_ylabel(ylabel)
    title='2018-2019年全省%s对比'%colName
    ax.set_title(title)
    ax.set_xticks(x)
    ax.set_xticklabels(labels)
    ax.legend()


    def autolabel(rects):
        """Attach a text label above each bar in *rects*, displaying its height."""
        for rect in rects:
            height = rect.get_height()
            ax.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')


    autolabel(rects1)
    autolabel(rects2)

    fig.tight_layout()

    plt.savefig(title+'.png')



for colName in colNames:
    print(colName)
    getPlot(colName)

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

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

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

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

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