|
From: Jianbao T. <jia...@gm...> - 2012-10-08 16:06:13
|
Thanks, Ben.
Your fix works when the view interval is greater than 1 minute, but not so
much when the view interval is less than one minute.
BTW, what I am trying to accomplish is to use matplotlib to plot
time-series data that can be as long as several days and as short as a few
milliseconds. So, do you think I am better off to handle the format
manually by myself instead of deriving the format from auto locator? I am
skeptical about the auto locator now because it doesn't seem to be designed
for intervals less than 1 second.
Cheers,
Jianbao
On Mon, Oct 8, 2012 at 6:16 AM, Benjamin Root <ben...@ou...> wrote:
>
>
> On Sun, Oct 7, 2012 at 6:47 PM, Jianbao Tao <jia...@gm...> wrote:
>
>> fig = figure()
>> tsta = num2epoch(date2num(datetime.datetime.now()))
>> tarr = tsta + arange(0, 60*60*0.5, 0.1) # half hour, dt =
>> 0.1 sec
>> x = np.array(num2date(epoch2num(tarr)))
>> nt = len(tarr)
>> y = randn(nt)
>>
>> ax = fig.add_subplot(111)
>> ax.plot(x, y)
>> fig.canvas.draw()
>>
>> # Make a formatter instance.
>> locator = mpl.dates.AutoDateLocator()
>> formatter = mpl.dates.AutoDateFormatter(locator)
>>
>> # Customize the scaling.
>> formatter.scaled = {
>> 365.0 : '%Y', # view interval > 356 days
>> 30. : '%b %Y', # view interval > 30 days but less than
>> 365 days
>> 1.0 : '%b %d', # view interval > 1 day but less than 30
>> days
>> 1./24. : '%H:%M', # view interval > 1 hour but less than 24
>> hours
>> 1./24./60. : '%M:%S', # view interval > 1 min but less than 1 hour
>> 1./24./60./60. : '%S', # view interval < 1 min
>> }
>>
>> # Apply the formatter and redraw the plot.
>> ax.get_xaxis().set_major_formatter(formatter)
>> fig.canvas.draw()
>>
>
> Confirmed. It seems like that code fell out of maintenance a bit
> somehow. Luckily, there is a quick fix to get your code working again.
> Just add the following two lines after you set the major formatter, but
> before your final draw:
>
> locator.set_axis(ax.xaxis)
> locator.refresh()
>
> Note, if you zoom in and out after that call, I doubt it will re-adjust
> your formatter. Could you file a bug report, please?
>
> Cheers!
> Ben Root
>
>
|