获取 x 轴网格以在 matplotlib 中显示

dar*_*dog 4 python matplotlib pandas

此代码绘制 Pandas Dataframe 中的特定列并提供倍数:

area_tabs=['12']
nrows = int(math.ceil(len(area_tabs) / 2.))
figlen=nrows*7 #adjust the figure size height to be sized to the number of rows
plt.rcParams['figure.figsize'] = 25,figlen 
fig, axs = plt.subplots(nrows, 2, sharey=False)
for ax, area_tabs in zip(axs.flat, area_tabs):
    actdf, aname = get_data(area_tabs)
    lastq,fcast_yr,projections,yrahead,aname,actdf,merged2,mergederrs,montdist,ols_test,mergedfcst=do_projections(actdf)
mergedfcst.tail(12).plot(ax=ax, title='Area: {0} Forecast for 2014 {1} \
 vs. 2013 actual of {2}'.format(unicode(aname),unicode(merged2['fcast']  [-1:].values),unicode(merged2['Units'][-2:-1].values)))
Run Code Online (Sandbox Code Playgroud)

数据框看起来大致如下:以日期作为索引

           Units    fcast
date        
2014-01-01   384     302
2014-02-01   NaN     343
2014-03-01   NaN     396
2014-04-01   NaN     415
2014-05-01   NaN     483
2014-06-01   NaN     513
Run Code Online (Sandbox Code Playgroud)

我得到一个看起来像这样的图: 水平背景网格(单位)显示良好,但我不知道如何显示垂直每月网格。我怀疑可能会被当作未成年人对待?但即便如此,也看不到在哪里将其指定给 pyplot/matplot?

在此输入图像描述

Pau*_*l H 5

ax.xaxis.grid(True, which=['major'|'minor'|'both'])在循环内部添加for应该可以解决问题。

这里最重要的是,尽可能避免plt状态机函数并直接对轴对象进行操作。

编辑:

不要太从字面上理解上面的代码片段。管道分隔的值只是伪正则表达式中提供的选项。如果您想要主要刻度,请使用:ax.xaxis.grid(True, which='major')