Adding minor ticks to pandas plot

Viewed 1703

I have the following code:

from pandas_datareader import data as web
import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(2, 1)
df = web.DataReader('F', 'yahoo')
df2 = web.DataReader('Fb', 'yahoo')
ax = df.plot(figsize=(35,15), ax=ax1)
df2.plot(y = 'Close', figsize=(35,15), ax=ax2)

plt.show()

This produces the chart which looks like this:

enter image description here

How can i change the minor ticks in pandas plot so it produces the x axis which looks like this: enter image description here

1 Answers

Check this code:

from pandas_datareader import data as web
import matplotlib.pyplot as plt
import matplotlib.dates as md

fig, (ax1, ax2) = plt.subplots(2, 1)
df = web.DataReader('F', 'yahoo')
df2 = web.DataReader('Fb', 'yahoo')
ax = df.plot(figsize=(35,15), ax=ax1)
df2.plot(y = 'Close', figsize=(35,15), ax=ax2)

for ax in (ax1, ax2):
    ax.xaxis.set_major_locator(md.MonthLocator(bymonth = range(1, 13, 6)))
    ax.xaxis.set_major_formatter(md.DateFormatter('%b\n%Y'))
    ax.xaxis.set_minor_locator(md.MonthLocator())
    plt.setp(ax.xaxis.get_majorticklabels(), rotation = 0 )

plt.show()

You can manage the xticks with the ax.xaxis methods. The above code produce this plot:

enter image description here

Related