python - Matplotlib: Formatting dates on the x-axis in a 3D Bar graph -
In view of this, how can you convert numerical data into a formatted date / time string in the X-axis? I have tried to use the ax.xaxis_date () function without success. I also tried to use plot_data (), which does not work for the 3D bar graph, here's a modified version of the sample code I'm trying to do:
< Code> Date as NP import matplotlib.dates as plut import numpy mpl_toolkits.mplot3d Import Axes3D import matplotlib.pyplot date = [dates.date2num (datetime.dat2num (datetime.datetime (2009312)), datesdate ( Datatom.datatated) (datetime.datetime (2009 6 9)), dates.date2num (datetime.datetime (2010,1,1)), #tc ...] fig = plt.figu Re () ax = x for x3 (fig), zip in zip (['r', 'g', 'b', 'y'], [30, 20, 10, 0]): xs = np.array (dates) ys = np.random.rand (20) ax.bar (xs, ys, zs = z, zdir = 'y', color = c, alpha = 0.8) ax.set_xlabel ( 'Date and time 'Ax.set_ylabel' ('series') ax.set_zlabel ('sum') plt.show ()
There may be some confusion here, Axes3D, the properties w_xaxis is w_yaxis and w_zaxis yaxis for axises instead of xaxix as always, etc.
UPDATE use the function for font does rmat label
NP import mpl_toolkits as plt import numpy as matplotlib.dates .mplot3d import Axes3D imported as dates datetime, ticker def random import matplotlib.ticker (as random_date the matplotlib.pyplot) to import .: Date = datetime.date (2008, 12,01) while 1: Date + = Datetime.timedelta (days = 30) yield (date) def format_date (x, pos = none): return dates. Num2date (x) .strftime ( '% Y-% m-% d') #use format dates r_d = random_date () some_dates = FuncFormatter [dates.date2num (r_d.next ()) i (0 in the range, 20)] fig = plt.figure () ax = x3d (fig, rect = [0,0.1,1,1]) # room for date label for zip (['r', 'g', ' B ',' Y '], [30, 20, 10, 0]): xs = np.array (some_dates) ys = np.random.rand (20) ax.bar (xs, ys, zs = z, zdir = 'y', color = C, alpha = 0.8, width = 8) ax.w_xaxis.set_major_locator (ticker. Fiksdloketr (Kuc_dets)) # I have all the dates on your Aksaksis ax.w_xaxis.set_major_formatter (ticker.FuncFormatter (format_date)) TA in the ax Want to l W_xaxis.get _ticklabels (): # create autofmt_xdate but w_xaxis tl.set_ha ('true') tl.set_rotation (30) ax.set_ylabel ('series') ax.set_zlabel ('sum') plt.show (# )
produces:
Comments
Post a Comment