2

I have data divided into different periods of time. The time-series is continuous for eg(one data set is for february then next on April and next in Jan 2016) But how can I reduce the gap of the year 2015 in between> It really just wastes the space and shrinks the graph

I can plot them together like the image below. Any way to remove the time-stamps in between?

enter image description here

Yash
  • 51
  • 2
  • 5
  • Manually set the xticks and xtick labels? – DavidG Sep 26 '17 at 12:12
  • 1
    Either you'll have to modify your x-values to remove the gaps, a adjust the x-ticks and labels, or you could create "gaps" in your x-axis as in https://stackoverflow.com/questions/32185411/break-in-x-axis-of-matplotlib – Diziet Asahi Sep 26 '17 at 12:17
  • 2
    As an alternative, you could generate a row of three subplots, one per continuous subrange (see https://matplotlib.org/examples/pylab_examples/subplots_demo.html for examples). – Hans Sep 26 '17 at 12:20
  • Is the date itself important? It's hard to tell from your question, but perhaps changing the x-axis to be "days from start of monitoring period" may be more appropriate. That would then allow all three of your series to be plotted along the same axis. Generally having a non-continuous date axis is very confusing to interpret and can be quite misleading. – Ken Syme Sep 26 '17 at 16:02
  • @DavidG that was my next idea. But I wanted to see if it's possible without doing that – Yash Sep 27 '17 at 03:57
  • Yeah I can do that @Hans,Thanks just wanted to know if it could be done in one graph – Yash Sep 27 '17 at 03:59
  • @KenSyme date is important because it helps in visualizing the place where values become outliers – Yash Sep 27 '17 at 04:00
  • @DizietAsahi I will look into it – Yash Sep 27 '17 at 04:03

2 Answers2

2

I found a solution, which isn't exactly what I was looking for but fulfills the purpose. I modified the solution posted of this link:

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()

Output: plot

SiHa
  • 7,830
  • 13
  • 34
  • 43
Yash
  • 51
  • 2
  • 5
0

If you are using a pandas DataFrame then this should work and it is a little tidier than the above answer:

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)
Little Bobby Tables
  • 4,466
  • 4
  • 29
  • 46