pandas .plot() x-axis tick frequency — how can I show more ticks?

No need to pass any args to MonthLocator. Make sure to use x_compat in the df.plot() call per @Rotkiv’s answer.

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

df = pd.DataFrame(np.random.rand(100,2), index=pd.date_range('1-1-2018', periods=100))
ax = df.plot(x_compat=True)
ax.xaxis.set_major_locator(mdates.MonthLocator())
plt.show()
  • formatted x-axis with set_major_locator

enter image description here

  • unformatted x-axis

enter image description here

Leave a Comment