熊猫:如何在pd.DataFrame.plot()中在x轴上显示小网格线

use*_*226 10 plot pandas

有没有办法在做pandas.DataFrame.plot()时控制网格格式?

具体来说,我想显示用于绘制具有日期时间索引的x轴的DataFrame的次要网格线.

这可以通过DataFrame.plot()吗?

df = pd.DataFrame.from_csv(csv_file, parse_dates=True, sep=' ')
Run Code Online (Sandbox Code Playgroud)

Bas*_*aan 17

也许这个功能去年不存在,但在版本0.19.2中你可以这样做:

df.plot(grid=True)
Run Code Online (Sandbox Code Playgroud)

文档.


beh*_*uri 13

这将以每周频率为S&p500绘制少量xticks和网格:

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' )
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Pau*_*l H 7

DataFrame.plot()应该Axes从 matplotlib 返回一个对象。

因此,使用ax = df.plot(...),您可以执行以下操作:

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