Showing milliseconds in matplotlib

I use matplotlib to build data as a function of time in the format hh: mm: ss.ms, where ms is milliseconds. However, I do not see milliseconds in the plot. Can I add them?

dates = matplotlib.dates.datestr2num(x_values) # convert string dates to numbers plt.plot_date(dates, y_values) # doesn't show milliseconds 
+7
source share
1 answer

The problem is that there is a class for formatting ticks, and plot_date sets this class to what you don't need: an automatic formatter that never traces milliseconds.

To change this, you need to go from matplotlib.dates.AutoDateFormatter to your own formatting. matplotlib.dates.DateFormatter (fmt) creates a formatter with a datetime.strftime format string. I'm not sure how to show it in milliseconds, but it will show microseconds that, I hope, will work for you; this is just one extra zero. Try this code:

 dates = matplotlib.dates.datestr2num(x_values) # convert string dates to numbers plt.plot_date(dates, y_values) # doesn't show milliseconds by default. # This changes the formatter. plt.gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%H:%M:%S.%f")) # Redraw the plot. plt.draw() 
+8
source

All Articles