3

The code in below plots a Pandas Time Series with milliseconds unit. However, Matplotlib stores this milliseconds timestamp as float64 internally, so that I can reformat them to hh:mm:ss as shown in below. Does anyone know how to manually convert Pandas timestamp to Matplotlib's float64 format? Thanks

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

x = np.arange(5000)
df = pd.DataFrame(x, columns=['y'])
df['timestamp'] = pd.to_datetime(df.index, unit='ms')
df.set_index('timestamp', inplace=True)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(df.index, df.y)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))

enter image description here

Matplotlib stores date in float64 format

print(ax.get_xlim())
>> (-2.892939814814815e-06, 6.075173611111111e-05)

print(type(ax.get_xlim()[0]))
>> numpy.float64

print(df.index[0])    # how to convert to -2.892939814814815e-06
>> 1970-01-01 00:00:00

print(df.index[-1])   # how to convert to 6.075173611111111e-05
>> 1970-01-01 00:00:04.999000

The mdates.date2num() doesn't work either

print(mdates.date2num(df.index[0]))
>> 0.0

print(mdates.date2num(df.index[-1]))
>> 5.785879629629629e-05

Final Update:

Sorry, I was wrong. The mdates.date2num() actually works! Initially I thought ax.get_xlim() was returning (x[0], x[-1]), but it actually returns something before x[0], and something after x[-1] so that the figure has some padding around the line. That's why it returns (-2.892939814814815e-06, 6.075173611111111e-05) instead of (0.0, 5.785879629629629e-05). Now I finally get it. Thanks guys!!

Scoodood
  • 583
  • 1
  • 5
  • 13

1 Answers1

2

The function mdates.num2date() is used to convert to the time managed by matplotlib. Kindly refer to this.

# print xlim in Matplotlib format
print(ax.get_xlim())
ts = mdates.num2date(ax.get_xlim()[0])
ts1 = mdates.num2date(ax.get_xlim()[1])
print(ts)
print(ts1)

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • thanks for your reply, but I would like to convert the pandas date to matplotlib num, for example from `1970-01-01 00:00:00` to `-2.892939814814815e-06` – Scoodood Sep 22 '20 at 15:11
  • You can convert dates to numbers with `mdates.date2num(df.index[1])`. – r-beginners Sep 23 '20 at 04:37
  • I had already tried your suggestion, the answer is not the same. – Scoodood Sep 23 '20 at 05:39
  • The first value obtained by `get_xlim()` indicates the value of the origin on the graph, so the value is before 1970 and the actual value is 1969-12-31 23:59:59.750050+00:00. What do you say does not fit with what? – r-beginners Sep 23 '20 at 05:58
  • hi @r-beginners, I think you are right, `get_xlim()` does not return `x[0]` and `x[-1]`. I think I finally get it now. Thanks a lot ^_^ – Scoodood Sep 23 '20 at 23:30