第一步先导入必要的包:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pandas import Series, DataFrame
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
第二步导入excel文件,确定好工作表:
xls_file = pd.ExcelFile('你的文件名.xlsx')
table = xls_file.parse('Sheet1')
第三步将年份设为索引:
df = table.set_index('时间')
第四步就是画图的细节了:
fig = plt.figure(figsize=(16,8))
ax1 = fig.add_subplot(111)
df['O3(ug/m3)'].plot(ax=ax1,style='b',alpha=0.7,label='O3(ug/m3)')
plt.xlabel('时间')
ax1.set_yticks(np.arange(0,200,20))
ax1.set_ylabel('O3(ug/m3)')
plt.legend(loc=1)
ax2 = ax1.twinx()
df['PM2.5(ug/m3)'].plot(ax=ax2,label='PM2.5(ug/m3)',style='r',alpha=0.7)
ax2.set_yticks(np.arange(0,300,20))
ax2.set_ylabel('PM2.5(ug/m3)')
plt.legend(loc=2)
plt.title('图例')
plt.show()
图如下:
|