2017-09-26 173 views
0

我有数据分为不同的时间段。时间序列是连续的,例如(一个数据集是2月份,接下来是4月份,下一次是2016年1月份)但是我怎样才能缩短2015年的差距>它真的只是浪费空间并缩小图表在matplotlib中同时绘制多个时间序列

我可以将它们绘制在一起,如下图所示。任何方式来删除之间的时间戳?

enter image description here

+0

手动设置xticks和xtick标签? – DavidG

+1

要么你必须修改你的x值来消除间隙,调整x-ticks和标签,或者你可以在x轴上创建“间隙”,就像在https://stackoverflow.com/questions/中一样。 32185411/matplotlib的x轴破坏 –

+2

作为替代方案,您可以生成一行三个子图,每个连续的子范围一个(请参阅https://matplotlib.org/examples/pylab_examples/subplots_demo.html示例)。 – Hans

回答

0

如果您使用的是熊猫数据帧那么这应该工作,它比上面的回答整洁一点:

​​
+0

不,DateTime索引很重要,我不想删除它。不得不保留日期时间索引是为什么问题发生在第一位 – Yash

+0

对不起,这不清楚。我的答案可能是最好的,但有一些整理。我将编辑。 – josh

+0

@Yash这应该工作,是更多的可读性和简洁性:) – josh

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