0

I have a problem when trying to plot a timeseries with matplotlib:

df = pd.read_csv('myfile.dat', skiprows=1)

#Change data type to datetime
date_format = '%Y-%m-%d %H:%M:%S'
df['TIME'] = pd.to_datetime(df['TIME'], format=date_format)

fig, ax = plt.subplots()
ax.plot(df['TIME'], df['Value'])
plt.show()

If I do:

print(df['TIME'][0])

the output is:

2022-04-16 14:32:00

which is the correct format! But when I plot everything, it changes to:

enter image description here

Can someone help me? I saw several times that you actually do not need Formatter and all that stuff.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • What is the type of the data? Do `type(df['TIME'])[0]` to find out. – Andrew Chisholm Apr 29 '22 at 10:45
  • type is: `` – Felixx_Ms Apr 29 '22 at 11:25
  • Note: plotting time series with pandas datetime data type axis sometimes causes trouble in combination with matplotlib formatters (which expect native Python datetime). You can try setting keyword `x_compat=True` to `df.plot` method in such cases. That feature is poorly documented unfortunately. – FObersteiner Apr 29 '22 at 12:24

1 Answers1

0

Matplotlib finds the best format according to your date. For example if there were only data per each hour of a day, it would display just the hours, if data there were only a data per day during a span of a week, it would display just the days.

The solution I propose is with mdates.DateFormatter:

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

df = pd.read_csv('file.csv')
print (df)

#Change data type to datetime
date_format = '%Y-%m-%d %H:%M:%S'
df['TIME'] = pd.to_datetime(df['TIME'], format=date_format)

fig, ax = plt.subplots()
ax.plot(df['TIME'], df['Value'])
plt.xticks(rotation=45, ha='right')
ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d %H:%M:%S"))
plt.show()

enter image description here

Joe
  • 12,057
  • 5
  • 39
  • 55
  • That works! Thank you. But another problem shows up, when it comes tochange the frequency of the ticks. I use: `plt.xticks(np.arange(0, len(df['TIME']), 60))` to adjust the frequency. The result (I tried it with several frequencies) always destroys the whole plot... Do you know why? – Felixx_Ms Apr 29 '22 at 13:51
  • try with something like: `ax.xaxis.set_major_locator(mdates.MinuteLocator(interval=15))` or `ax.xaxis.set_major_locator(mdates.HourLocator(interval=1))` etc. If it solve the problem, please consider to accept the answer – Joe Apr 29 '22 at 13:58