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

8
  • Manually set the xticks and xtick labels? Commented Sep 26, 2017 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 stackoverflow.com/questions/32185411/… Commented Sep 26, 2017 at 12:17
  • 2
    As an alternative, you could generate a row of three subplots, one per continuous subrange (see matplotlib.org/examples/pylab_examples/subplots_demo.html for examples). Commented Sep 26, 2017 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. Commented Sep 26, 2017 at 16:02
  • @DavidG that was my next idea. But I wanted to see if it's possible without doing that Commented Sep 27, 2017 at 3:57

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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)

3 Comments

No, DateTime Index is important, I don't want to remove it. Having to keep date-time index is why the problem occurred in first place
Sorry, this wasn't clear. I your answer might be best but with some tidying. I will edit.
@Yash this should work and is a lot more readable and concise :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.