1

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;

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

enter image description here

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.

GoBlue_MathMan
  • 1,048
  • 2
  • 13
  • 20
  • May not be the best strategy but since you're only concerned with 9am to 4pm, couldn't you just filter 4pm to 9am out of the data you're plotting? – Abdou Aug 30 '16 at 15:26
  • its a time series plot so matplotlib automatically fills in between. That's not my data its just a line connecting two points. – GoBlue_MathMan Aug 30 '16 at 15:29
  • Oh I see. [this](http://stackoverflow.com/questions/5656798/python-matplotlib-is-there-a-way-to-make-a-discontinuous-axis) could help make the line and its corresponding axis area disappear, but it may not be exactly what you're after. – Abdou Aug 30 '16 at 15:38
  • If your data is closely spaced, you could cheat and simply use points: change `'b-'` to `'b.'` – Nick T Aug 30 '16 at 15:55
  • 'b.' does not work...space is still there – GoBlue_MathMan Aug 30 '16 at 16:02

1 Answers1

1

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 data['Date_Time'], I would make sure your index is reset via data.reset_index(). 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 plt.xticks() as seen in this demo case. Put together, I would expect it to look something like this:

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'])

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. :/

StarFox
  • 529
  • 3
  • 10