在matplotlib中同时绘制多个时间序列

时间:2017-09-26 11:58:25

标签: python pandas matplotlib

我将数据分为不同的时间段。时间序列是连续的,例如(一个数据集是2月,然后是下一个4月和下一个2016年1月)但是我怎样才能减少2015年之间的差距>它真的只是浪费空间并缩小图形

我可以将它们绘制在一起,如下图所示。有没有办法删除中间的时间戳?

enter image description here

2 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,这不是我想要的,但实现了目的。我修改了this链接发布的解决方案:

fig=plt.figure()
fig,(ax,ax2,ax3) = plt.subplots(1,3,sharey=True)
fig.subplots_adjust(wspace=0.05)
fig.set_size_inches(20, 3, forward=True)

ax.plot(healthyde['timestamp'],healthyde['value'],label='healthy',color='g')
ax2.plot(healthynde['timestamp'],healthynde['value'],label='detection',color='y')
ax3.plot(healthylde['timestamp'],healthylde['value'],label='fault',color='r')

ax.set_xlim(healthyde['timestamp'].iloc[0], healthyde['timestamp'].iloc[-1] )
ax2.set_xlim(healthynde['timestamp'].iloc[0], healthynde['timestamp'].iloc[-1] )
ax3.set_xlim(healthylde['timestamp'].iloc[0], healthylde['timestamp'].iloc[-1] )

labels = ax.get_xticklabels()
for label in labels:
    label.set_rotation(30)

labels = ax2.get_xticklabels()
for label in labels:
    label.set_rotation(30)

labels = ax3.get_xticklabels()
for label in labels:
    label.set_rotation(30)

ax.legend()
ax2.legend()
ax3.legend()

plt.show()

<强>输出: plot

答案 1 :(得分:0)

如果您使用的是pandas DataFrame,那么这应该可行,并且它比上面的答案稍微整洁:

fig = plt.figure()
fig, axes = plt.subplots(1, 3, sharey=True)

for (col, data), ax, c in zip(df.iteritems(), axes):
    data.dropna().plot(ax=ax, rot=30, legend=True)
相关问题