user2846226
user2846226

Reputation: 447

Pandas: How to display minor grid lines on x-axis in pd.DataFrame.plot()

Is there a way to control grid format when doing pandas.DataFrame.plot()?

Specifically i would like to show the minor gridlines for plotting a DataFrame with a x-axis which has a DateTimeIndex.

Is this possible through the DataFrame.plot()?

df = pd.DataFrame.from_csv(csv_file, parse_dates=True, sep=' ')

Upvotes: 28

Views: 59412

Answers (3)

Bastiaan
Bastiaan

Reputation: 4672

Maybe this feature didn't exist last year, but in version 0.19.2 you can do:

df.plot(grid=True)

See the doc.

Upvotes: 57

behzad.nouri
behzad.nouri

Reputation: 77951

this will plot S&p500 with minor xticks and grids at weekly frequency:

import pandas.io.data as web
ts = web.DataReader("^GSPC", "yahoo", start=dt.date( 2013, 6, 1 ))[ 'Adj Close' ]

ax = ts.plot()
xtick = pd.date_range( start=ts.index.min( ), end=ts.index.max( ), freq='W' )
ax.set_xticks( xtick, minor=True )
ax.grid('on', which='minor', axis='x' )
ax.grid('off', which='major', axis='x' )

enter image description here

Upvotes: 29

Paul H
Paul H

Reputation: 68146

DataFrame.plot() should return an Axes object from matplotlib.

So with ax = df.plot(...), you can do:

ax.xaxis.grid(True, which='minor', linestyle='-', linewidth=0.25, ...)

Upvotes: 7

Related Questions