1

I'm trying to plot a small set of data that contains dates and values. I noticed that my x-axis labels (dates) are not displayed correctly - only when I have a smaller dataset.

import pandas as pd
import matplotlib.pyplot as plt

dates = ['2018-02-01', '2018-02-02', '2018-02-03', '2018-02-04', '2018-02-05']
values = [22, 18, 26, 12, 15]
df = pd.DataFrame({'Dates' : dates, 'Values' : values})

#Convert to datetime64
df['Dates'] = pd.to_datetime(df['Dates'])

#Plot data
fig, ax = plt.subplots()
plt.plot(df['Dates'], df['Values'])
fig.autofmt_xdate()

The following plot is produced (notice the x-axis labels):

plot

However, when I have a larger set of data I do not have this issue:

dates = ['2018-02-01', '2018-02-02', '2018-02-03', '2018-02-04', '2018-02-05', '2018-02-06', '2018-02-07', '2018-02-08', '2018-02-09', '2018-02-10']
values = [22, 18, 26, 12, 15, 13, 20, 16, 22, 19]
df = pd.DataFrame({'Dates' : dates, 'Values' : values})

#Convert to datetime64
pd.to_datetime(df['Dates'])

#Plot data
fig, ax = plt.subplots()
plt.plot(df['Dates'], df['Values'])
fig.autofmt_xdate()

Which yields this plot:

plot2

What's going on? How do I address this?

Thanks to Thomas for explaining what Matplotlib is trying to do. The snobby guy got upset and marked this as duplicate after deleting his useless comment. Here is the solution for anyone else that is interested:

Solution

days = mdates.DayLocator()
myformat = mdates.DateFormatter('%Y-%m-%d')

dates = ['2018-02-01', '2018-02-02', '2018-02-03', '2018-02-04', '2018-02-05']
values = [22, 18, 26, 12, 15]
df = pd.DataFrame({'Dates' : dates, 'Values' : values})

#Convert to datetime64
df['Dates'] = pd.to_datetime(df['Dates'])

#Plot data
fig, ax = plt.subplots()
plt.plot(df['Dates'], df['Values'])

ax.xaxis.set_major_locator(days)
ax.xaxis.set_major_formatter(myformat)

fig.autofmt_xdate()
NRH
  • 311
  • 2
  • 5
  • 14
  • 2
    The upper plot shows month, day and hour, while the lower one shows year, month and day. This is because matplotlib has to guess what you want and somehow decides that this is the best way to label your x axis. If you want the upper plot to look the same as the lower one, you have to adjust the tick locater and the tick formatter. – Thomas Kühn Feb 08 '18 at 18:52
  • Thanks for the explanation. I didn't think of 'hours' being an option since it was not part of my datetime. – NRH Feb 08 '18 at 19:24

0 Answers0