Skip to content Skip to sidebar Skip to footer

Matplotlib Time Axis With Continuous Hours

I want to format my x-axis in the way '%H:%M' but with continuous hours (e.g. 2 days = 48:00) like in this example: How I want it The closest attempt I could made is this example:

Solution 1:

You are not plotting any actual dates here. It hence makes sense to not try to format those values as dates. Instead, plot the numbers as they are and use your custom format of choice.

import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import numpy as np

dataY = np.array([0,1,2,3,4,5,6,7,8,9])
dataX = np.array([0.1,0.5,0.8,1.2,1.3,1.6,1.9,2.1,2.2,2.5]) #Time values 1h = 1/24

fig, ax = plt.subplots()

deftimeformat(x,pos=None):
    h = int(x*24.)
    m = int((x*24.-h)*60)
    return"{:02d}:{:02d}".format(h,m)

ax.xaxis.set_major_formatter(mticker.FuncFormatter(timeformat))

plt.plot(dataX,dataY)
plt.ylabel('Y-Values')
plt.xlabel('Time [hh:mm]')

plt.show()

enter image description here

Post a Comment for "Matplotlib Time Axis With Continuous Hours"