簡體   English   中英

將第二個數據系列添加到Matplotlib中的子圖

[英]Add a second data series to a sub-plot in Matplotlib

因此,我有這段代碼,可以很好地產生子圖,並以網格的形式排列。 數據位於Pandas DataFrames中。

我不知道如何向子圖中添加第二個數據系列? 所以現在我繪制了fullyrs.Units並且想添加merged2.fcast.plot(style='r')我似乎缺少獲取該子圖圖的參考? 我嘗試過的一些事情最終都超出了“循環”之外的情節。

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

您已經使用plt.subplots創建了ax ,因此您只需要將ax=ax傳遞給merged2.fcast.plot pandas調用即可,而無需設置ax=...來創建新軸。 例如。

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

您可能已經知道這一點,但是也可以使用fig, axs = plt.subplots(nrows, 2, sharey=False, figsize=(25, 7*nrows))來設置圖形大小,而不是在rcparams全局設置它。 當然,在其余的代碼中,您可能還需要控制其他圖形。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM