Skip to main content
 首页 » 编程设计

python之Matplotlib 副标题打印在旧副标题上

2024年11月01日9lexus

我在 ubuntu 13.10 64 位中使用 matplotlib 1.2.1

我有一个创建和保存绘图的函数,我多次调用该函数。然而,在某些情况下,副标题会覆盖旧副标题,造成困惑。 我认为这与以下问题有关,但我没能理解该问题的答复: Matplotlib suptitle prints over old title

我的代码如下:

# ======================================================================= 
def plotMultivariable(scenarios_h, variables_h, region, title, filename): 
 
  nvar = len(variables_h) 
  nscen = len(scenarios_h) 
  plt.figure(1) 
  maintitle = unicode(title, 'utf_8') 
  plt.suptitle(maintitle, fontsize=16, ha='center') 
  for i in range(nvar):     
    plt.subplot(nvar,1,i) 
    title = unicode(variables[variables_h[i]][0], 'utf_8') 
    ylabel = unicode(variables[variables_h[i]][1], 'utf_8') 
    plt.title(title) 
    plt.ylabel(ylabel) 
    for scenario in scenarios_h: 
      for spGroup in spAggregates.keys(): 
        serieName = unicode(spGroup + " - " + scenario, 'utf_8') 
        serieColor = scenarios[scenario] 
        serieLineType = spAggregates[spGroup][2] 
        serieWidth = spAggregates[spGroup][3] 
        key = region, variables_h[i], scenario, spGroup 
        y = odata[key] 
        plt.plot(x, y, serieLineType, label=serieName, linewidth=serieWidth, color=serieColor) 
  plt.subplots_adjust(hspace=0.6) 
  plt.savefig(chartoutdir+"/"+filename) 
  #plt.show() 

我调用这个函数:

plotMultivariable(['reference','vRegEnd075','vRegFixed','vRegFromHr'],['expReturns','vReg','vol','hV'],'11000','Management effect','management.png') 
plotMultivariable(['reference','withRisk06','withRisk08','withRisk10'],['expReturns','vReg','vol','hV'],'11000','Risk effect','risk.png') 
plotMultivariable(['nonspatial','reference','withVariance'],['expReturns','vReg','vol','hV'],'11000','Spatial explicit simulations','space.png') 

这是结果图。如果我查看它们而不是保存它们就可以了。任何提示或帮助理解引用答案中的回复?谢谢..

请您参考如下方法:

最后你应该关闭你的图形以正确清除所有内容;只需添加

plt.close() 

在你的函数结束时,它应该可以工作。