Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

graphing milliseconds in python matplotlib.dates

I have time tagged data formatted using datetime.datetime.strptime that includes milliseconds. The data can span from a few minutes to a few hours. When I plot using pyplot and zoom in, it seems the minimum tick mark is 1 second. It appears that matplotlib's TickHelper RRuleLocator only has a SecondLocator. Is there a way to include millisecond resolution when zoomed in?

receivedTime = datetime.datetime.strptime(str(data[1]), "%H:%M:%S.%f")

fig=plt.figure()
ax=fig.add_axes([0.1,0.1,.8,.8])
fig.autofmt_xdate()

ax.plot(times, prices, color='blue', lw=1, drawstyle='steps-post', label = prices)
plt.show()
like image 697
needHelp Avatar asked Sep 27 '26 12:09

needHelp


1 Answers

Matplotlib uses Matlab like numbers (seconds since 1970) for dates. If you have milliseconds, you must convert the dates to 'number':

import matplotlib.dates as mdates
ntimes = mdates.num2epoch(times / 1000.0)

and plot ntimes instead of times.

like image 131
Pablo Navarro Avatar answered Sep 30 '26 02:09

Pablo Navarro