Wednesday, January 17, 2018

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

Leave a Comment

I am plotting time series using pandas .plot() and want to see every month shown as an x-tick.

Here is the dataset structure data set

Here is the result of the .plot()

enter image description here

I was trying to use examples from other posts and matplotlib documentation and do something like

ax.xaxis.set_major_locator(    dates.MonthLocator(revenue_pivot.index, bymonthday=1,interval=1)) 

But that removed all the ticks :(

I also tried to pass xticks = df.index, but it has not changed anything.

What would be the rigth way to show more ticks on x-axis?

2 Answers

Answers 1

The right way to do that described here Using the x_compat parameter, it is possible to suppress automatic tick resolution adjustment

df.A.plot(x_compat=True)

Answers 2

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() 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment