Dates along the x-axis quad-set

I would like to use matplotlib and Axes.pcolormesh to create the plot. My problem is that I want to have dates along the x axis:

 import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) qmesh = ax.pcolormesh(times,mlt,data.T) fig.colorbar(qmesh,ax=ax) 

in this code, times is a numpy array (1D) created using matplotlib.dates.date2num . This creates a perfectly reasonable plot, except that the x-axis marks values โ€‹โ€‹of the order of 1e5 instead of dates / times in the format '%H:%M' . We appreciate any suggestions. Thanks.

+4
source share
2 answers

In addition to the answer you already found, you can do ax.xaxis_date() , which is actually equivalent.

As a quick example (which also uses fig.autofmt_xdate() to rotate x-labels):

 import matplotlib.pyplot as plt import matplotlib.dates as mdates import datetime as dt # Generate some data x = mdates.drange(dt.datetime(2012, 01, 01), dt.datetime(2013, 01, 01), dt.timedelta(weeks=2)) y = np.linspace(1, 10, 20) data = np.random.random((y.size, x.size)) # Plot fig = plt.figure() ax = fig.add_subplot(111) qmesh = ax.pcolormesh(x, y, data) fig.colorbar(qmesh,ax=ax) ax.axis('tight') # Set up as dates ax.xaxis_date() fig.autofmt_xdate() plt.show() 

enter image description here

+5
source

Turns out I needed:

 import matplotlib.dates as dates ax.xaxis.set_major_formatter(dates.DateFormatter('%H:%M')) 

.

+2
source

Source: https://habr.com/ru/post/1484295/


All Articles