使用pandas绘制两个日期直方图

时间:2018-01-18 08:01:36

标签: python pandas

我有两个日期时间系列,我试图与共享的X轴并排绘制。

dates1 = ['2015-02-02', '2016-06-29', '2016-06-01', '2015-07-19', '2016-08-17', '2016-11-22',
'2016-07-24', '2016-10-30', '2015-02-01', '2017-01-29', '2015-03-19', '2016-09-06',
'2016-11-23', '2016-06-21', '2016-10-05', '2016-02-23', '2016-11-24', '2016-10-05',
'2015-07-16', '2016-06-07', '2016-07-31', '2016-11-01', '2016-11-02', '2016-08-16',
'2015-06-09', '2016-04-11', '2017-02-09', '2015-05-20', '2016-05-17', '2016-09-12',
'2015-08-05', '2017-02-19']

dates2 = ['2016-03-22', '2016-03-16', '2015-07-02', '2016-09-13', '2014-09-04', '2016-07-12',
'2016-05-08', '2016-02-18', '2014-07-10', '2016-05-10', '2016-05-02', '2016-11-20',
'2015-05-19', '2016-01-06', '2016-06-21', '2015-03-25', '2016-06-09', '2016-12-07',
'2016-10-18', '2016-03-27', '2017-03-19', '2016-10-27', '2017-01-12', '2015-12-31',
'2016-05-05', '2016-07-17', '2016-07-10', '2017-06-14', '2015-12-27', '2016-03-01', 
'2016-05-04', '2017-05-15']

ser1 = pd.Series(dates1, dtype=np.datetime64)
ser2 = pd.Series(dates2, dtype=np.datetime64)

fig, axes = plt.subplots(1, 2, figsize=(20, 10), sharex=True)
ser1.groupby([ser1.dt.year, ser1.dt.month]).count().plot(kind='bar', ax=axes[0])
ser2.groupby([ser2.dt.year, ser2.dt.month]).count().plot(kind='bar', ax=axes[1])
plt.show()

enter image description here

如图所示,ser1似乎有(2014, 7)个值,但其第一个实际值为2015-02-01。作为参考,这两个图有sharex=False

fig, axes = plt.subplots(1, 2, figsize=(20, 10), sharex=False)
ser1.groupby([ser1.dt.year, ser1.dt.month]).count().plot(kind='bar', ax=axes[0])
ser2.groupby([ser2.dt.year, ser2.dt.month]).count().plot(kind='bar', ax=axes[1])
plt.show()

enter image description here

解决这个问题的任何简单方法,无需手动限制X轴?

1 个答案:

答案 0 :(得分:2)

您可以连接ser1ser2 groupby计数结果,这会导致NaN的行显示在两个系列的缺失日期中。然后简单地用{0} fillna并继续使用相同的绘图方法:

sgp1 = ser1.groupby([ser1.dt.year, ser1.dt.month]).count()
sgp2 = ser2.groupby([ser2.dt.year, ser2.dt.month]).count()

df = pd.concat([sgp1, sgp2], axis=1).fillna(0)

fig, axes = plt.subplots(1, 2, figsize=(20, 10), sharex=True)

df[0].plot(kind='bar', ax=axes[0])
df[1].plot(kind='bar', ax=axes[1])
plt.show()

结果: enter image description here

相关问题