Matplotlib axis time formatter

158 views Asked by At

I have a logarithmic axis with time in seconds on with labels

10^1 sec, 10^2 sec, 10^3 sec, 10^4 sec ...

Like it is similarily possible with EngFormatter I want to have the axis ticks and labels in seconds, then minutes, then hours, then days, like this:

0 sec, 30 sec, 1 min, 30 min, 1 hour, 12 hour, 1 day ...

How do I get axis ticks and labels like that?

1

There are 1 answers

0
Henri On BEST ANSWER

I was able to create the axis I wanted

plot

with this code:

def to_time(x, position):
    x = float(x)
    timeunit = 'sec'
    if x >= 60:
        x = x/60
        timeunit = 'min'
        if x >= 60:
            x = x/60
            timeunit = 'h'
    # you can modify the actual string here
    string = '%.0f%s' % (x,timeunit)
    return string

#... do your plots here

ax.set_yscale('log')
tickerMajor = LogLocator(base=60)
tickerMinor = LogLocator(base=60, subs=[10,20,30,40,50])
tickLabels = FuncFormatter(to_time)
ax.yaxis.set_major_locator(tickerMajor)
ax.yaxis.set_minor_locator(tickerMinor)
ax.yaxis.set_major_formatter(tickLabels)

Hope this helps somebody. However this only works for sec, min and hours, but not days, months etc as those are no longer easily reachable with log60.