From: Ryan M. <rm...@gm...> - 2009-05-07 18:38:29
|
On Mon, May 4, 2009 at 9:49 AM, Olivier Benoist < oli...@er...> wrote: > Hi, > I'm new with matplotlib. > > I need to make a graph with the X axis represents time in hours and > minutes. My script don't works, I want to display all the values of time > that I have. > I use a list of string like this : > t=['0015', '0030', '0045', '0100', '0115', '0130', '0145', '0200', > '0215', '0230', '0245', '0300', '03 > 15', '0330', '0345', '0400', '0415', '0430', '0445', '0500', '0515', > '0530', '0545', '0600', '0615', > '0630', '0645', '0700', '0715', '0730', '0745', '0800', '0815', '0830', > '0845', '0900', '0915', '09 > 30', '0945', '1000', '1015', '1030', '1045', '1100', '1115', '1130', > '1145', '1200', '1215', '1230', > '1245', '1300', '1315', '1330', '1345', '1400', '1415'] > > ax.plot(t, y) > > I tried to convert hours and minutes to the base 100 ( , it works but I > can not show on the x-axis the hours, minutes. > I tried to use plot_date, but I don't understand "x and/or y can be a > sequence of dates represented as float days since 0001-01-01 UTC." > > Could you help me, please ? You don't necessarily need to use plot_date. Try this: import numpy as np import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatter, MultipleLocator times = ['0015', '0030', '0045', '0100', '0115', '0130', '0145', '0200', '0215', '0230', '0245', '0300', '0315', '0330', '0345', '0400', '0415', '0430', '0445', '0500', '0515', '0530', '0545', '0600', '0615', '0630', '0645', '0700', '0715', '0730', '0745', '0800', '0815', '0830', '0845', '0900', '0915', '0930', '0945', '1000', '1015', '1030', '1045', '1100', '1115', '1130', '1145', '1200', '1215', '1230', '1245', '1300', '1315', '1330', '1345', '1400', '1415'] # Conver the string time values into the corresponding number of minutes minutes = np.array([int(t[:2])*60 + int(t[2:]) for t in times]) y = np.random.rand(*minutes.shape) plt.plot(minutes, y) ax = plt.gca() # Set the formatter to take a value in minutes and convert to hour:minute ax.xaxis.set_major_formatter(FuncFormatter( lambda t,p : '%02d:%02d' % (t//60, t%60))) # Set up placing tick marks every 15 minutes ax.xaxis.set_major_locator(MultipleLocator(15)) # Used to rotate all of the ticks so that they fit on the plot. # ha='center' aligns them better to the tick marks fig = plt.gcf() fig.autofmt_xdate(rotation=90, ha='center') plt.show() Ryan -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma Sent from Norman, Oklahoma, United States |