多变量柱状图

数据同样利用折线图示例中所使用的AO.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import pandas as pd
import matplotlib.pyplot as plt
#数据读取
ao = pd.read_csv("AO.txt",sep='\s+',header=None, names=['year','month','AO'])
ao_dec = ao[ao.month==12]
ao_jan = ao[ao.month==1]
ao_feb = ao[ao.month==2]
#创建Figure及Axes
fig = plt.figure(figsize=(10, 8))
ax1 = fig.add_subplot(1,1,1)
ax1.set_title('1979-2019 DJF AO Index')
#绘制柱状图
ax1.bar(np.arange(2000,2020,1)-0.25,ao_dec.AO[50:],width=0.25,color='r',label='Dec')
ax1.bar(np.arange(2000,2020,1),ao_jan.AO[50:],width=0.25,color='b',label='Jan')
ax1.bar(np.arange(2000,2020,1)+0.25,ao_feb.AO[50:],width=0.25,color='g',label='Feb')
#添加0值参考线
ax1.axhline(0,c='k')
#添加图例
ax1.legend()
plt.show()

输出图形如下:

image-20200702161610554