Skip to content Skip to sidebar Skip to footer

Adding Minor Ticks To Pandas Plot

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 = w

Solution 1:

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


Post a Comment for "Adding Minor Ticks To Pandas Plot"