I have my data in an array of the following structure,
[[1293606162197, 0, 0], [1293605477994, 63, 0], [1293605478057, 0, 0], [1293605478072, 2735, 1249], [1293606162213, 0, 0], [1293606162229, 0, 0]]
The first column is epoch time (in ms
), second is y1
and third is y2
. I need a plot with the time on the x-axis, and y1
and y2
on left and right y-axes.
I have been scouring through the documentation but couldn't find any way to get my x-axis ticks to display both date and time, like "28/12 16:48", i.e., "date/month hour:min". All the documentation helps me with is to display dates alone, but that is not what I want. Any help would be appreciated on this.
And, if it may seem like, this is not homework. It is actually a follow up to my previous question, Reading and graphing data read from huge files.
MatPlotLib with Python Create a figure and a set of subplots using subplots() method. Plot the dataframe using plot method, with df's (Step 1) time and speed. To edit the date formatting from %d-%m-%d to %d:%m%d, we can use set_major_formatter() method. Set the formatter of the major ticker.
In X-axis we should have a variable of DateTime. In Y-axis we can have the variable which we want to analyze with respect to time. plt. plot() method is used to plot the graph in matplotlib.
I hope this helps. I've always had a hard time with matplotlib's dates. Matplotlib requires a float format which is days since epoch. The helper functions num2date
and date2num
along with python builtin datetime
can be used to convert to/from. The formatting business was lifted from this example. You can change an axis on any plot to a date axis using set_major_formatter
.
import numpy as np from matplotlib import pyplot as plt from matplotlib import dates import datetime a = np.array([ [1293605162197, 0, 0], [1293605477994, 63, 0], [1293605478057, 0, 0], [1293605478072, 2735, 1249], [1293606162213, 0, 0], [1293606162229, 0, 0]]) d = a[:,0] y1 = a[:,1] y2 = a[:,2] # convert epoch to matplotlib float format s = d/1000 ms = d-1000*s # not needed? dts = map(datetime.datetime.fromtimestamp, s) fds = dates.date2num(dts) # converted # matplotlib date format object hfmt = dates.DateFormatter('%m/%d %H:%M') fig = plt.figure() ax = fig.add_subplot(111) ax.vlines(fds, y2, y1) ax.xaxis.set_major_locator(dates.MinuteLocator()) ax.xaxis.set_major_formatter(hfmt) ax.set_ylim(bottom = 0) plt.xticks(rotation='vertical') plt.subplots_adjust(bottom=.3) plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With