偶然看到网上国家统计数据,利用Python数据分析自己做了几种图表练习。主要采用Pandas来做数据统计,matplotlib来做图表可视化。
下面图表数据来源于网络。
柱状图和折线图叠加
代码如下:
代码语言:python代码运行次数:0复制
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.patches import Patch
from matplotlib.lines import Line2D
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)
width = 0.35 # the width of the bars: can also be len(x) sequence
colNames=["二氧化硫","氮氧化物","烟尘","烟尘_"]
dictCol={"二氧化硫":'SO2',"氮氧化物":'NO2',"烟尘":'PM25',"烟尘_":'PM10'}
regionTypes=["省辖市","市辖区"]
def getPlot(colName,regionType):
colName2=dictCol[colName]
print(colName,regionType,colName2)
colName=colName.strip('_')
df.sort_values('月份')
fig, ax = plt.subplots(figsize=(10,6))
labels=df.月份
y1=df['2018年%s_%s'%(colName,regionType)].apply(round)
y2=df['2019年%s_%s'%(colName,regionType)].apply(round)
x = np.arange(len(labels)) # the label locations
rects1=ax.bar(x-width/2.0, y1, width, label='2018年%s排放量'%colName,color="tab:brown")
rects2=ax.bar(x width/2.0, y2, width, label='2019年%s排放量'%colName,color="tab:red")
ax.set_ylabel('吨')
title='全省2018-2019年%s排放和%s监测值对比(%s)'%(colName,colName2,regionType)
ax.set_title(title)
ax.set_xticks(x)
ax.set_xticklabels(labels)
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)
ax2 = plt.twinx()
ax2.set_ylabel('微克/立方米')
ymax=np.max([df[colName2 '_2018年'].max(),df[colName2 '_2019年'].max()])*(1 0.1)
ax2.set_ylim(bottom=0,top=ymax)
p1=ax2.plot(df[colName2 '_2018年'],color="brown")
p2=ax2.plot(df[colName2 '_2019年'],color="red")
ax2.yaxis.set_tick_params(direction='out')
print(colName2)
legend_elements = [
Patch(facecolor='tab:brown', edgecolor='b',label='2018年%s排放量'%colName),
Patch(facecolor='tab:red', edgecolor='b',label='2019年%s排放量'%colName),
Line2D([0], [0], color='brown', lw=2, label='2018年%s监测值'%colName2),
#Line2D([0], [0], marker='o', color='w', label='Scatter', markerfacecolor='g', markersize=15),
Line2D([0], [0], color='red', lw=2, label='2019年%s监测值'%colName2),
]
ax.legend(handles=legend_elements, loc='best')
plt.savefig(title '.png')
plt.close()
for colName,regionType in itertools.product(colNames,regionTypes):
print(colName,regionType)
getPlot(colName,regionType)