Pandas:如何在 pd.DataFrame.plot() 中在 x 轴上显示次要网格线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20616754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas: How to display minor grid lines on x-axis in pd.DataFrame.plot()
提问by user2846226
Is there a way to control grid format when doing pandas.DataFrame.plot()?
在执行 pandas.DataFrame.plot() 时有没有办法控制网格格式?
Specifically i would like to show the minor gridlines for plotting a DataFrame with a x-axis which has a DateTimeIndex.
具体来说,我想显示用于绘制具有 DateTimeIndex 的 x 轴的 DataFrame 的次要网格线。
Is this possible through the DataFrame.plot()?
这可以通过 DataFrame.plot() 实现吗?
df = pd.DataFrame.from_csv(csv_file, parse_dates=True, sep=' ')
回答by Bastiaan
回答by behzad.nouri
this will plot S&p500 with minor xticks and grids at weekly frequency:
这将以每周频率绘制带有较小 xticks 和网格的 S&p500:
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' )


回答by Paul H
DataFrame.plot()should return an Axesobject from matplotlib.
DataFrame.plot()应该Axes从 matplotlib返回一个对象。
So with ax = df.plot(...), you can do:
因此ax = df.plot(...),您可以执行以下操作:
ax.xaxis.grid(True, which='minor', linestyle='-', linewidth=0.25, ...)
ax.xaxis.grid(True, which='minor', linestyle='-', linewidth=0.25, ...)

