Changing the formatting of a datetime axis in matplotlib

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

# sample data
N = 30
drange = pd.date_range("2014-01", periods=N, freq="MS")
np.random.seed(365)  # for a reproducible example of values
values = {'values':np.random.randint(1,20,size=N)}
df = pd.DataFrame(values, index=drange)

fig, ax = plt.subplots()
ax.plot(df.index, df.values)

# use formatters to specify major and minor ticks
_ = plt.xticks(rotation=90)    

enter image description here

Leave a Comment