1

So I have this code which produces subplots nicely, laid out in a grid. Data is in Pandas DataFrames.

I can't figure out how to add a second data series to the subplots? So now I plot fullyrs.Units and I want to add merged2.fcast.plot(style='r') I seem to be missing a reference to get the figure for the subplot? A few things I've tried end up with plots outside the "loop".

#area_tabs=list(map(str, range(1, 28)))
area_tabs=['1','2','3']
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):  
    fullyrs,lastq,fcast_yr,projections,yrahead,aname,actdf,merged2,mergederrs,montdist,ols_test, mergedfcst=do_projections(actdf,aname)
#    ax=merged2.fcast.plot(style='r') <<<< I want to get this to plot in the same sub-plot as below
    fullyrs.Units.plot(ax=ax, title='Area: {0} Forecast for 2014 {1} vs. Actual 2013 of {2} '.format(unicode(aname),unicode(merged2['fcast'][-1:].values), lastyrtot))
3
  • You've already created ax with plt.subplots so don't you just need to pass ax=ax to merged2.fcast.plot instead of setting ax=...? Commented Feb 2, 2014 at 22:08
  • @Rauparaha, so right! Thanks! If you make answer I can mark it Commented Feb 2, 2014 at 22:20
  • Done, glad to be able to help! Commented Feb 2, 2014 at 22:26

1 Answer 1

1

You've already created ax with plt.subplots so you just need to pass ax=ax to merged2.fcast.plot pandas call instead of setting ax=..., which creates a new axis. eg.

for ax, area_tabs in zip(axs.flat, area_tabs):  
    fullyrs,lastq,fcast_yr,projections,yrahead,aname,actdf,merged2,mergederrs,montdist,ols_test, mergedfcst=do_projections(actdf,aname)
    merged2.fcast.plot(ax=ax, style='r') <<<< I want to get this to plot in the same sub-plot as below
    fullyrs.Units.plot(ax=ax, title='Area: {0} Forecast for 2014 {1} vs. Actual 2013 of {2} '.format(unicode(aname),unicode(merged2['fcast'][-1:].values), lastyrtot))

You probably already know this but you could also set the figure size with fig, axs = plt.subplots(nrows, 2, sharey=False, figsize=(25, 7*nrows)) instead of setting it globally in the rcparams. Of course, you may have other figures you want to control in the rest of your code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the last tip also! still very early learning MPL/pyplot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.