x-axes time format

fig.show() needed here.

Ah ok, great, many thanks.

In the case the x is already HH:MM:SS that's a whole other story I
guess?

anyway,
cheers
marco

···

On Fri, Jun 10, 2011 at 09:00:28AM -0400, Scott Lasley wrote:

One way would be to use a matplotlib.ticker.FuncFormatter

import matplotlib.pyplot as plt
import matplotlib.ticker

def HMSFormatter(value, loc):
    h = value // 3600
    m = (value - h * 3600) // 60
    s = value % 60
    return "%02d:%02d:%02d" % (h,m,s)

fig = plt.figure()
sp = fig.add_subplot(111)
xaxis = sp.get_xaxis()
xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(HMSFormatter))
seconds = range(12341,12641,30)
data = range(10)
sp.plot(seconds, data)
fig.canvas.draw()

--
   (o_ It rocks: LINUX + Command-Line-Interface
   //\ GPG: 0x59D90F4D
   V_/_ http://www.calmar.ws

Why not just use an array of datetime.timedelta objects? I believe matplotlib already supports this, does automatic formatting and even allows you to easily modify how the formatting is done.

Ben Root

···

On Fri, Jun 10, 2011 at 11:01 AM, calmar c. <mac@…3622…> wrote:

On Fri, Jun 10, 2011 at 09:00:28AM -0400, Scott Lasley wrote:

One way would be to use a matplotlib.ticker.FuncFormatter

import matplotlib.pyplot as plt

import matplotlib.ticker

def HMSFormatter(value, loc):

h = value // 3600
m = (value - h * 3600) // 60
s = value % 60
return "%02d:%02d:%02d" % (h,m,s)

fig = plt.figure()

sp = fig.add_subplot(111)

xaxis = sp.get_xaxis()

xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(HMSFormatter))

seconds = range(12341,12641,30)

data = range(10)

sp.plot(seconds, data)

fig.canvas.draw()

fig.show() needed here.

Ah ok, great, many thanks.

In the case the x is already HH:MM:SS that’s a whole other story I

guess?

anyway,

cheers

marco

I was not able to figure out how it would support them
(datetime.timedelta objects). datetime.datetime objects only as
far as I saw.

So I would have to create some artificial datetime + add the
timedeltas (seconds) to it and providing that to the plot I guess,
right?

So far the solution to just show i.e. 70sec as 00:01:20 via some
little function is almost more sympathetic to me (so far at least).

Cheers and thanks,
marco

···

On Fri, Jun 10, 2011 at 11:10:02AM -0500, Benjamin Root wrote:

   Why not just use an array of datetime.timedelta objects?� I believe
   matplotlib already supports this, does automatic formatting and even
   allows you to easily modify how the formatting is done.

--
   (o_ It rocks: LINUX + Command-Line-Interface
   //\ GPG: 0x59D90F4D
   V_/_ http://www.calmar.ws

Why not just use an array of datetime.timedelta objects?� I believe

matplotlib already supports this, does automatic formatting and even

allows you to easily modify how the formatting is done.

I was not able to figure out how it would support them

(datetime.timedelta objects). datetime.datetime objects only as

far as I saw.

That’s right, I forgot about that. Usually, my data would have a starting point anyway, and I just use timedeltas after that point.

So I would have to create some artificial datetime + add the

timedeltas (seconds) to it and providing that to the plot I guess,

right?

You might be able to get away with using a datetime.time object and add timedeltas. The formatting should be what you would like to see.

So far the solution to just show i.e. 70sec as 00:01:20 via some

little function is almost more sympathetic to me (so far at least).

Whatever works best for you, that’s more important. Note that matplotlib has some special treatment of date, time and datetime objects to do extra things for you. It might be a bit tricky at first, but it becomes easier to use later.

Admittedly, these following examples are for dates (and might even need to be updated…)
http://matplotlib.sourceforge.net/examples/api/date_demo.html

http://matplotlib.sourceforge.net/examples/api/date_index_formatter.html

There are others as well.

Note that probably half that code can probably be removed for v1.0.x. When I did my graphs a few months ago, I don’t recall needing to specify any formatters. Maybe I should dig up my code and update those examples.

Cheers,
Ben Root

···

On Fri, Jun 10, 2011 at 12:50 PM, calmar c. <mac@…3622…> wrote:

On Fri, Jun 10, 2011 at 11:10:02AM -0500, Benjamin Root wrote:

Hi Ben and Scott and all,

   Admittedly, these following examples are for dates (and might even need to
   be updated...)
   [2]http://matplotlib.sourceforge.net/examples/api/date_demo.html
   [3]http://matplotlib.sourceforge.net/examples/api/date_index_formatter.html

   There are others as well.

I see, thanks (all your examples here are pretty helpful for
understanding matplotlib somebit better - even so I'm using the
Formatter-Function now).

marco

···

On Fri, Jun 10, 2011 at 10:50:21PM -0500, Benjamin Root wrote:

--
   (o_ It rocks: LINUX + Command-Line-Interface
   //\ GPG: 0x59D90F4D
   V_/_ http://www.calmar.ws