Matplotlib dateformatter is not formatting the date as wished

1.2k Views Asked by At

I have this pandas dataframe and I would like to plot it by only showing the day, hour, min and secs.

enter image description here

As time, I have a Unix TimeStamp in ms and I convert it to the format you already see in the dataframe.

frm = mdates.DateFormatter('%Y-%m-%d %H:%M:%S')
df['startTime'] = pd.to_datetime((df['startTime']), unit='ms')
print(df)

plt.plot_date(df['startTime'], df['parameter.value'], linestyle='solid')
plt.gcf().autofmt_xdate()
plt.gca().xaxis.set_major_formatter(frm)

plt.show()

Although I format it with the DateFormatter, still the mins and the secs do not appear in the plot.

enter image description here

1

There are 1 best solutions below

2
On

If the X axis is a string, the date/time format can be displayed without rounding.

import matplotlib.dates as mdates
import matplotlib.pyplot as plt

df['startTime'] = pd.to_datetime(df['startTime'], format='%Y-%m-%d %H:%M:%S.%f')
print(df)

plt.plot_date(df['startTime'].astype(str), df['parameter.value'], xdate=True, linestyle='solid')
# plt.gcf().autofmt_xdate()
# plt.gca().xaxis.set_major_formatter(frm)

plt.xticks(rotation=90)
plt.show()

enter image description here