偶然看到网上国家统计数据,利用Python数据分析自己做了几种图表练习。主要采用Pandas来做数据统计,matplotlib来做图表可视化。
下面图表数据来源于网络。
柱状图
代码如下:
代码语言:python代码运行次数:0复制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)