dartdog
dartdog

Reputation: 10862

getting the x axis grid to show in matplotlib

This code plots specific columns in a Pandas Dataframe And provides for multiples:

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

with a dataframe that looks roughly like: With date as an index

           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

I get a plot that looks like: The horizontal background grid (Units) shows fine but I can't figure out how to get the vertical monthly grid to show. I suspect it may be treated as a minor? but even then can't see where to specify it to pyplot/matplot?

enter image description here

Upvotes: 4

Views: 2237

Answers (1)

Paul H
Paul H

Reputation: 68146

Adding ax.xaxis.grid(True, which=['major'|'minor'|'both']) inside of your for loop should do the trick.

The main thing here is that you avoid plt state machine functions where possible and operate on the axes objects directly.

Edit:

Don't take the above code snippet too literally. The pipe-seperated values are just the options presented in pseudo-regex. If you want the major ticks, use: ax.xaxis.grid(True, which='major')

Upvotes: 5

Related Questions