简体   繁体   English

用熊猫绘制两个日期直方图

[英]Plotting two date histograms with pandas

I have two datetime series, which I'm trying to plot side by side with a shared X-axis. 我有两个日期时间系列,我试图与一个共享的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()

在此处输入图片说明

As seen in the image, it appears that ser1 has (2014, 7) values, but its first actual value is 2015-02-01 . 如图所示, ser1似乎具有(2014, 7) ser1 (2014, 7)值,但其第一个实际值是2015-02-01 For reference, the two plots with sharex=False : 供参考,这两个图具有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()

在此处输入图片说明

Any simple way to solve this, without manually limiting the X-axis? 有什么简单的方法可以解决此问题,而无需手动限制X轴?

You can concatenate the ser1 and ser2 groupby count results, which will cause rows of NaNs to appear in missing dates of both series. 您可以将ser1ser2 groupby计数结果串联ser2 ,这将导致NaN的行出现在两个系列的缺失日期中。 Then simply fillna with zeros and proceed with the same plot method: 然后简单地用零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()

Result: 结果: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM