Ignoring Time gaps larger than x mins Matplotlib in Python
<p>I get data every 5 mins between 9:30am and 4pm. Most days I just plot live intraday data. However, sometimes I want a historical view of lets says 2+ days. The only problem is that during 4pm and 9:30 am I just get a line connecting the two data points. I would like that gap to disappear. My code and an example of what is happening are below;</p>

<pre><code>fig = plt.figure()
plt.ylabel('Bps')
plt.xlabel('Date/Time')
plt.title(ticker)
ax = fig.add_subplot(111)
myFmt = mdates.DateFormatter('%m/%d %I:%M')
ax.xaxis.set_major_formatter(myFmt)
line, = ax.plot(data['Date_Time'],data['Y'],'b-')
</code></pre>

<p><a href="http://i.stack.imgur.com/NcLrz.png" rel="nofollow"><img src="http://i.stack.imgur.com/NcLrz.png" alt="enter image description here"></a></p>

<p>I want to keep the data as a time series so that when i scroll over it I can see the exact date and time.</p>

<p>So it looks like you're using a pandas object, which is helpful. Assuming you have filtered out any time between 4pm and 9am in <code>data['Date_Time']</code>, I would make sure your index is reset via <code>data.reset_index()</code>. You'll want to use that integer index as the under-the-hood index that matplotlib actually uses to plot the timeseries. Then you can manually alter the tick labels themselves with <code>plt.xticks()</code> as seen in <a href="http://matplotlib.org/examples/ticks_and_spines/ticklabels_demo_rotation.html" rel="nofollow">this demo case</a>. Put together, I would expect it to look something like this:</p>

<pre><code>data = data.reset_index(drop=True) # just to remove that pesky column 

fig, ax = plt.subplots(1,1)
ax.plot(data.index, data['Y'])
plt.ylabel('Bps')
plt.xlabel('Date/Time')
plt.title(ticker)
plt.xticks(data.index, data['Date_Time'])
</code></pre>

<p>I noticed the last statement in your question just after posting this. Unfortunately, this "solution" doesn't track the "x" variable in an interactive figure. That is, while the time axis labels adjust to your zoom, you can't know the time by cursor location, so you'd have to eyeball it up from the bottom of the figure. :/</p>

python matplotlib time-series graphing timeserieschart