Title: Matplotlib plots in the wrong data format eventhough it is a datetime object
Tags: python;pandas;datetime;matplotlib;time-series
Question: 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:
Can someone help me? I saw several times that you actually do not need Formatter and all that stuff.
Comment: What is the type of the data? Do `type(df['TIME'])[0]` to find out.

Comment: type is: ``

Comment: 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.


Here is another answer: 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(&quot;%Y-%m-%d %H:%M:%S&quot;))
plt.show()
Comment for this answer: 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
Comment for this answer: 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?

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:04/29/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><pandas><datetime><matplotlib><time-series>', 'Title': 'Matplotlib plots in the wrong data format eventhough it is a datetime object', 'ViewCount': 55, 'id': 72056616, 'user_AboutMe': None, 'user_AccountId': 25140818.0, 'user_DisplayName': 'Felixx_Ms', 'user_Reputation': 1.0, 'user_Views': 1.0, 'user_id': 18987166.0}}