Creating an xaxis with minutes

He there,

I’m new to matplotlib but have really been appreciating the thorough documentation and examples online. I’ve never worked with matlab either so I’m stumbling my way around a bit, but have managed to get some basic plots working with my wxPython program.

I’m plotting some simple time/temp curves. For the time, I have just the number of seconds, starting at 0 and going up to about 1200 (20 minutes). I’m trying to format the xaxis so that there is a tick mark every one minute, and format the tick marks so they display %M:%S. The code below doesn’t give me any formatting or tick marks on the xaxis. It seems like this should be fairly obvious how to accomplish, but I’m not getting it.

What am I doing wrong here?

xvals = range(1200)
yvals = list of temps

self.mainline = self.ax.plot(xvals, yvals, lw=2, color=‘red’)[0]

locator = matplotlib.dates.MinuteLocator()
self.ax.xaxis.set_major_locator(locator)
self.ax.xaxis.set_minor_locator(locator)

formatter = matplotlib.dates.DateFormatter(’%M:%S’)
self.ax.xaxis.set_major_formatter(formatter)
self.ax.xaxis.set_minor_formatter(formatter)

draw the canvas

Any help appreciated…thanks!
BZ

Hi,

I'm not a frequent user of matplotlib.dates module, so other expert
may give you a better answer.
My understanding is that, for the date time formatting, the (x-) data
needs to be days (if not datetime instance) from some reference point
(1, 1, 1? I'm not sure).

The easiest way I can think of in your case is

from matplotlib.dates import datetime, SEC_PER_DAY
ordinal_today=datetime.datetime.today().toordinal()
xvals = ordinal_today + np.arange(1200, dtype="d")/SEC_PER_DAY

Also, you'd better comment out the set_minor_locator and
set_minot_formatter calls, unless you want the same x-label drawn
twice.

IHTH,

-JJ

···

On Tue, Apr 7, 2009 at 3:47 PM, Brian Zambrano <brianz@...287...> wrote:

He there,

I'm new to matplotlib but have really been appreciating the thorough
documentation and examples online. I've never worked with matlab either so
I'm stumbling my way around a bit, but have managed to get some basic plots
working with my wxPython program.

I'm plotting some simple time/temp curves. For the time, I have just the
number of seconds, starting at 0 and going up to about 1200 (20 minutes).
I'm trying to format the xaxis so that there is a tick mark every one
minute, and format the tick marks so they display %M:%S. The code below
doesn't give me *any* formatting or tick marks on the xaxis. It seems like
this should be fairly obvious how to accomplish, but I'm not getting it.

What am I doing wrong here?

xvals = range(1200)
yvals = list of temps

self.mainline = self.ax.plot(xvals, yvals, lw=2, color='red')[0]

locator = matplotlib.dates.MinuteLocator()
self.ax.xaxis.set_major_locator(locator)
self.ax.xaxis.set_minor_locator(locator)

formatter = matplotlib.dates.DateFormatter('%M:%S')
self.ax.xaxis.set_major_formatter(formatter)
self.ax.xaxis.set_minor_formatter(formatter)

# draw the canvas

Any help appreciated...thanks!
BZ

------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

You can also do this without converting to ordinal by hand:

from datetime import datetime, timedelta
today = datetime.today()

xvals = [today + timedelta(seconds=s) for s in range(1200)]
# Matplotlib can use lists, but you can also make this into a numpy object array
xvals = np.array(xvals)

Since matplotlib’s date formatter and locator require absolute times, you could also just make your own locator and formatter functions for your relative time values:

import matplotlib.ticker as mticker

def minsec(sec, unused):
minutes = sec // 60
sec = sec - minutes * 60
return ‘%d:%02d’ % (minutes, sec)

locator = mticker.MultipleLocator(60)
formatter = mticker.FuncFormatter(minsec)

Ryan

···

On Tue, Apr 7, 2009 at 4:29 PM, Jae-Joon Lee <lee.j.joon@…1003…7…> wrote:

Hi,

I’m not a frequent user of matplotlib.dates module, so other expert

may give you a better answer.

My understanding is that, for the date time formatting, the (x-) data

needs to be days (if not datetime instance) from some reference point

(1, 1, 1? I’m not sure).

The easiest way I can think of in your case is

from matplotlib.dates import datetime, SEC_PER_DAY

ordinal_today=datetime.datetime.today().toordinal()

xvals = ordinal_today + np.arange(1200, dtype=“d”)/SEC_PER_DAY


Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
Sent from Enterprise, AL, United States

This is exactly what I was looking for…thank you! With this example (and the docs) I finally understand how the Locator and Formatter classes work. Now, I just can do this:

    locator = mticker.MultipleLocator(60)
    formatter = mticker.FuncFormatter(lambda x, y: '%02d:%02d' % divmod(x, 60))

    self.ax.xaxis.set_major_locator(locator)
    self.ax.xaxis.set_major_formatter(formatter)

Thanks again,
BZ

···

On Wed, Apr 8, 2009 at 7:33 AM, Ryan May <rmay31@…287…> wrote:

import matplotlib.ticker as mticker

def minsec(sec, unused):
minutes = sec // 60
sec = sec - minutes * 60
return ‘%d:%02d’ % (minutes, sec)

locator = mticker.MultipleLocator(60)
formatter = mticker.FuncFormatter(minsec)

Ryan