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):
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:
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()