I have a list of integers representing a time series in milliseconds. Each value was obtained by calling int(time.time() * 1000)
and appending to the list.
Now I would like to make a plot using matplotlib that will display the milliseconds time series on the x-axis in the format "%H:%M:%S"
.
I tried using these values along with a matplotlib data formatter by setting: plt.gca().xaxis.set_major_formatter(dates.DateFormatter("%H:%M:%S"))
but I got the following error when attempting to plot: OverflowError: int too big to convert.
I have also tried using already preformatted time series values as strings by calling time.strftime('%H:%M:%S', time.gmtime(time.time()))
and appending to the list instead with no luck, resulting in this problem: Using categorical units to plot a list of strings that are all parsable as floats or dates. If these strings should be plotted as numbers, cast to the appropriate data type before plotting.
Could anyone please help me to plot milliseconds in a human readable format on the x-axis?
You can convert time values (which are ms since epoch in UTC) using epoch2num
. Then you can use the standard DateFormatter
.
import matplotlib.pyplot as plt
import matplotlib as mpl
import time
now_ms = int(time.time() * 1000)
x = [now_ms+t for t in range(0,10_000,1000)]
plt.plot([mpl.dates.epoch2num(t/1000) for t in x], range(10))
plt.gca().xaxis.set_major_formatter(mpl.dates.DateFormatter("%H:%M:%S"))
You should use matplotlib version 3.3.1 or newer.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments