10

I am currently generating the plot below: plot

with this code:

ax = plt.subplots()
ax.plot(intra.to_pydatetime(), data)
plt.title('Intraday Net Spillover')
fig.autofmt_xdate()

where intra.to_pydatetime() is a: <bound method DatetimeIndex.to_pydatetime of <class 'pandas.tseries.index.DatetimeIndex'> [2011-01-03 09:35:00, ..., 2011-01-07 16:00:00] Length: 390, Freq: None, Timezone: None>

So the dates go from 2011-01-03 09:35:00, increments by 5 minutes until 16:00:00, and then jumps to the next day, 2011-01-04 09:35:00 until 2011-01-04 16:00:00, and so on.

How can I avoid plotting the gaps between 16:00:00 and 9:30:00 on the following day? I don't want to see these straight lines.

UPDATE:

I will try this to see if it works.

3
  • 1
    I don't know if that's not too complicated, but I did that once by seperating the plotted series into multiple series and plotted each on its own, as I don't know how pandas should know what's a gap and what isn't. Commented Dec 3, 2014 at 8:48
  • Addendum: In one plot, but as seperate series, with same colour and linestyle, only giving a legend entry to the first. Commented Dec 3, 2014 at 8:53
  • 1
    For other users, although question is old, please review my answer and accept it if it worked. Commented Dec 20, 2015 at 19:32

2 Answers 2

8

Simply set the two values defining the line you don't want to see as NaN (Not a Number). Matplotlib will hide the line between the two values automatically.

Check out this example : http://matplotlib.org/examples/pylab_examples/nan_test.html

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

3 Comments

A single float('NaN') is enough to create a gap.
this is just hide, but still showing the 'gap'
That is not true, the gaps are still there.
2

Try to resample your dataframe. For example :

df.plot()

gives me that result : plot

and now with resample:

df = df.resample('H').first().fillna(value=np.nan)

plot after resample

Comments

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.