From: Goyo <goy...@gm...> - 2008-10-13 22:56:20
|
El dom, 12-10-2008 a las 13:22 -1000, Eric Firing escribió: > Goyo wrote: > > Hi all, > > > > I'm plotting time series and using a custom format for x tick labels > > because I want to see both date and time. But calling xlim makes the > > default format return -- only dates or only times, it depends on the > > displayed interval. > > > > I can get my preferred format back by calling set_major_formatter again, > > but this is inconvenient for interactive use. > > > > I wonder if there is a better way for changing x limits while keeping > > tick labels format unchanged. > > > > I'm using matplotlib 0.98.3 > > > > Thanks > > > > Goyo > > > > > > Sample code -- you need a combination of python shell and matplotlib GUI > > which allows for interactive use in order to get this working as > > expected: > > > > ---------------------------------------- > > > > from matplotlib import pyplot > > from matplotlib import dates > > from datetime import datetime, timedelta > > from numpy import random > > > > pyplot.ion() > > > > # create data > > dstart = datetime(2008, 1, 1, 0, 0) > > dend = datetime(2008, 1, 5, 23, 50) > > delta = timedelta(minutes=10) > > x = dates.drange(dstart, dend, delta) > > y = random.random_sample(len(x)) > > > > # plot data > > pyplot.plot(x, y) > > > > # format x tick labels > > axis = pyplot.gca() > > fig = pyplot.gcf() > > axis.xaxis.set_major_formatter(dates.DateFormatter('%Y-%b-%d %H:%M')) > > fig.autofmt_xdate() > > > > # draw the figure > > pyplot.draw() > > > > -------------------------------------- > > > > Look at the tick labels, they show both date and time even if you zoom > > and pan using the GUI. > > > > Now if I want to look at the second day: > > > > dstart = datetime(2008, 1, 1, 0, 0) > > dend = datetime(2008, 1, 1, 23, 50) > > pyplot.xlim(dstart, dend) > > > > And the format has changed. > > I don't see this problem using ipython -pylab with mpl from svn on > linux, gtkagg backend. What version, backend, and platform are you using? matplotlib 0.98.3-3ubuntu1~ppa1 from Benjamin Drung's ppa (<http://ppa.launchpad.net/bdrung/ubuntu>) on Ubuntu Hardy. It was the standard python shell and TKAgg, but I get the same result with ipython -pylab and GTKAgg. Indeed the format change can be "documented": print axis.xaxis.get_major_formatter() pyplot.xlim(dstart, dend) print axis.xaxis.get_major_formatter() The first print: <matplotlib.dates.DateFormatter instance at 0xb6e2462c> and the second: <matplotlib.dates.AutoDateFormatter instance at 0x884dc4c> I did some debugging and realized that xaxis.units is None after plotting, so xlim triggers a call to xaxis.set_units which sets the default formatter (and units, whatever it means). If I set my formatter again, xaxis.units is not None anymore but still xaxis._update_axisinfo sets the default formatter. Anyway I worked around this by writing my own version of xlim which first saves the formatter, then sets xlim and sets the saved formatter again. Goyo |