From: Jiri P. <po...@ph...> - 2005-12-13 16:05:45
|
Hallo, I have array (ut) of numbers represents unix time (number of seconds from 1. 1. 1970) of some evenets: ... 1127164705 1127199439 1127199439 1127199494 1127199640 1127199651 ... I want to plot histogram of this events. I used: from pylab import * n, bins, patches = hist( ut, 50 ) setp(patches, 'facecolor', 'g', 'alpha', 0.75) axis([ 0.9999*amin(ut), 1.0001*amax(ut), 0, 1.1*amax(n) ]) show() But I want to display labels on x-axis's tics as a date human readable date (for example "Dec 13"). Can anybody help me? -- JP |
From: <rom...@ya...> - 2005-12-13 18:20:47
|
Jiri Polcar wrote: > > But I want to display labels on x-axis's tics as a date human readable > date (for example "Dec 13"). > I adapted this from finance_demo.py. You will probably have to adjust it to better suit your needs. It assumes the ut array is sorted. from pylab import * from matplotlib.dates import DateFormatter, WeekdayLocator, HourLocator, \ DayLocator, MONDAY, timezone import time,datetime ut = [1127164705, 1127199439, 1127199439, 1127199494, 1127199640, 1127199651] date1 = apply(datetime.date,time.localtime(ut[0])[0:3]) date2 = apply(datetime.date,time.localtime(ut[-1])[0:3]) mondays = WeekdayLocator(MONDAY) weekFormatter = DateFormatter('%b %d') # Eg, Jan 12 alldays = DayLocator() ax = subplot(111) ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(alldays) ax.xaxis.set_major_formatter(weekFormatter) dates = [] for u in ut: year,month,day = time.localtime(u)[0:3] dates.append(datetime.date(year,month,day).toordinal()) n, bins, patches = hist( dates, 50 ) setp(patches, 'facecolor', 'g', 'alpha', 0.75) #axis([ 0.9999*amin(ut), 1.0001*amax(ut), 0, 1.1*amax(n) ]) show() |
From: Jiri P. <po...@ph...> - 2005-12-13 18:55:27
|
It works perfectly. Thanke you. How can I set x-axis range? In case of my real data the histogram is not "centred" and epmty spaces was too huge. In my previous way works: axis([ 0.9999*amin(ut), 1.0001*amax(ut), 0, 1.1*amax(n) ]) -- JP |
From: John H. <jdh...@ac...> - 2005-12-14 03:47:00
|
>>>>> "Jiri" == Jiri Polcar <po...@ph...> writes: Jiri> Hallo, I have array (ut) of numbers represents unix time Jiri> (number of seconds from 1. 1. 1970) of some evenets: Jiri> ... 1127164705 1127199439 1127199439 1127199494 Jiri> 1127199640 1127199651 ... matplotlib interprets dates as days since 0000-00-00 rather than seconds since the epoch, so you need to convert epoch time with the epoch2num function in matplotlib.dates and then plot with plot_dates dnum = epoch2num(ut) plot_date(dnum, y) Jiri> I want to plot histogram of this events. I used: Jiri> from pylab import * n, bins, patches = hist( ut, 50 ) Jiri> setp(patches, 'facecolor', 'g', 'alpha', 0.75) axis([ Jiri> 0.9999*amin(ut), 1.0001*amax(ut), 0, 1.1*amax(n) ]) show() Jiri> But I want to display labels on x-axis's tics as a date Jiri> human readable date (for example "Dec 13"). If you are not happy with the default date ticking and formatting provided by plot_date, you can set you own date tick locator and formatter. Read about tick locators and formatters here http://matplotlib.sf.net/matplotlib.ticker.html and date specific tick locator and formatters here http://matplotlib.sf.net/matplotlib.dates.html and in the online Users Guide. See also the *date*.py examples in the examples directory, http://matplotlib.sf.net/examples Jiri> It works perfectly. Thanke you. How can I set x-axis range? Jiri> In case of my real data the histogram is not "centred" and Jiri> epmty spaces was too huge. In my previous way works: Jiri> axis([ 0.9999*amin(ut), 1.0001*amax(ut), 0, 1.1*amax(n) Use epoch2num again xlim(epoch2num(amin(ut)), epoch2num(amax(ut))) Note that if you pick the appropriate date locator (which plot_date tries to do for you automagically), you probably do not need to set the xlim explicitly, since this is the job of the tick locator's autoscale method. JDH |
From: Jiri P. <po...@ph...> - 2005-12-14 11:56:05
|
On Tue, Dec 13, 2005 at 09:39:57PM -0600, John Hunter wrote: > > Use epoch2num again > > xlim(epoch2num(amin(ut)), epoch2num(amax(ut))) > Thanke you. -- JP |