【问题标题】:getting the x axis grid to show in matplotlib让 x 轴网格在 matplotlib 中显示
【发布时间】:2014-01-27 20:23:57
【问题描述】:

此代码在 Pandas 数据框中绘制特定列并提供倍数:

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)))

使用大致如下的数据框:以日期为索引

           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

我得到一个看起来像这样的情节: 水平背景网格(单位)显示良好,但我不知道如何显示垂直每月网格。我怀疑它可能被视为未成年人?但即使那样也看不到在哪里指定它到pyplot/matplot?

【问题讨论】:

  • len(area_tabs) = 1 时你制作两个轴有什么原因吗?
  • 是的,因为这是测试代码,最终会有一系列标签 (20+)

标签: python matplotlib pandas


【解决方案1】:

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

这里的主要内容是您尽可能避免使用plt 状态机函数并直接对轴对象进行操作。

编辑:

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

【讨论】:

  • whacky,如果我把 ax.xaxis.grid(True, which=['minor']) 或者如果我使用专业,(或者如果我完全使用你的代码......在第一行for 循环我得到一个错误 AttributeError: 'list' object has no attribute 'lower' from in matplotlib 如果我只使用 ax.xaxis.grid(True) 没有变化
  • 直接使用你的代码我得到 TypeError: unsupported operand type(s) for |: 'str' and 'str'
  • @dartdog "minor" 和 "major" 等只是选项。不要按字面意思使用。
  • 这就是我的想法,但是我自己使用每个都得到了“无属性”错误
  • @dartdog 那是因为当函数需要标量字符串时,您将它们放在列表中
猜你喜欢
  • 2014-10-30
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 2020-07-26
  • 2020-11-08
相关资源
最近更新 更多