matplotlib intelligent axis labels for timedelta

Could you provide an example? Is this what you want:

import datetime                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                               
import numpy as np                                                                                                                                                                                                                                                             
import pylab as plt                                                                                                                                                                                                                                                            
import matplotlib                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                               
fig = plt.figure()                                                                                                                                                                                                                                                             
ax = fig.add_subplot(111)                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                               
x = np.linspace(0, 300)  # 5 minutes                                                                                                                                                                                                                                                      
y = np.random.random(len(x))                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                               
ax.plot(x, y)                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                               
def timeTicks(x, pos):                                                                                                                                                                                                                                                         
    d = datetime.timedelta(seconds=x)                                                                                                                                                                                                                                          
    return str(d)                                                                                                                                                                                                                                                              
formatter = matplotlib.ticker.FuncFormatter(timeTicks)                                                                                                                                                                                                                         
ax.xaxis.set_major_formatter(formatter)                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                               
plt.show()

Example output

It uses pythons timedelta. With 864000 seconds the above will result in “10 days, 10:00:00”. You can of course stuff more advanced formatting into the timeTicks() function above.

Leave a Comment