anjo
anjo

Reputation: 345

How to set the xticklabels for date in matplotlib

I am trying to plot values from two list. The x axis values are date. Tried these things so far

year = [20070102,20070806,20091208,20111109,20120816,20140117,20140813]
yvalues = [-0.5,-0.5,-0.75,-0.75,-0.5,-1.25,-1.25]

dates = [datetime.datetime.strptime(str(int(date)),'%Y%m%d') for date in year]

fig, axes = plt.subplots(1, 1, figsize=(16, 9), dpi=100)

line1, = axes.plot(dates,yvalues, lw=2, marker='*', color='r')
axes.legend([line1],['VALUES'],loc=1)
axes.grid()
axes.set_xlabel('YEAR')
axes.set_ylabel('VALUES')

yticks = [-2.0,-1.75,-1.5,-1.25,-1.0,-0.75,-0.5,-0.25,0.0]
axes.set_yticks(yticks)
axes.set_yticklabels(["$%.2f$" % y for y in yticks]);

axes.set_xticks(dates)
#axes.set_xticklabels()

The figure seems good. But unable to label the x axis ticks correctly. I am trying to label x axis with something like 2007-01-02 or 2007 Jan.

Thanks in advance

Upvotes: 7

Views: 22785

Answers (2)

user2138149
user2138149

Reputation: 16484

Pain-free solution. Minimal complication.

fig, ax = plt.subplots()

# load data, ensure 'date' column is a `datetime` object
df = pandas.read_csv(f'data.csv')
df['date'] = pandas.to_datetime(df['date'])

ax.plot(df['date'], df['y_values'])

# every 7 days
ax.set_xticks(df['date'][::7])
ax.set_xticklabels(labels=df['date'][::7].dt.strftime('%Y-%m-%d'), rotation=25)

It seems that set_xticks sets some (abstract concept of) ticks. It's sort of defining "where ticks should be" but not "what the label text should be".

set_xticklabels then sets the label text.

You need to be careful to be consistent here. It's easy to produce a plot which has nonsensical labels. All the function calls have to be consistent.

If somebody has a better explanation as to why we need both function calls set_xticks and set_xticklabels, please let me know.

If you want to set the limits as well

Ensure you call set_xticks and set_xticklabels before you call ax.set_xlim((date(y, m, d), date(y1, m1, d1)). Otherwise, the plot will be inconsistent and have nonsensical labels.

Upvotes: 0

unutbu
unutbu

Reputation: 879063

You can control the format of the dates using a DateFormater:

import matplotlib.dates as mdates
xfmt = mdates.DateFormatter('%Y-%m-%d')
axes.xaxis.set_major_formatter(xfmt)

import datetime as DT
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

year = [20070102,20070806,20091208,20111109,20120816,20140117,20140813]
yvalues = [-0.5,-0.5,-0.75,-0.75,-0.5,-1.25,-1.25]

dates = [DT.datetime.strptime(str(int(date)),'%Y%m%d') for date in year]

fig, axes = plt.subplots(1, 1, figsize=(16, 9), dpi=100)

line1, = axes.plot(dates,yvalues, lw=2, marker='*', color='r')
axes.legend([line1],['VALUES'],loc=1)
axes.grid()
axes.set_xlabel('YEAR')
axes.set_ylabel('VALUES')

yticks = [-2.0,-1.75,-1.5,-1.25,-1.0,-0.75,-0.5,-0.25,0.0]
axes.set_yticks(yticks)
axes.set_yticklabels(["$%.2f$" % y for y in yticks]);

xfmt = mdates.DateFormatter('%Y-%m-%d')
axes.xaxis.set_major_formatter(xfmt)
axes.set_xticks(dates)
plt.xticks(rotation=25)
plt.show()

yields

enter image description here

Upvotes: 13

Related Questions