Python Matplotlib suptitle打印在旧suptitle上

Python Matplotlib suptitle打印在旧suptitle上,python,matplotlib,Python,Matplotlib,我在ubuntu 13.10 64位中使用matplotlib 1.2.1 我有一个创建和保存绘图的函数,我多次调用该函数。然而,在某些情况下,suptitle被拉长在旧标题之上,造成混乱。 我认为这与以下问题有关,但我未能理解该问题的回答: 我的代码如下: # ======================================================================= def plotMultivariable(scenarios_h, variables_

我在ubuntu 13.10 64位中使用matplotlib 1.2.1

我有一个创建和保存绘图的函数,我多次调用该函数。然而,在某些情况下,suptitle被拉长在旧标题之上,造成混乱。 我认为这与以下问题有关,但我未能理解该问题的回答:

我的代码如下:

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

在你的例子中,可能会让你感到不安的是,
matplotlib
是状态感知的(我不知道确切的单词),并且在已经创建的对象的背景中跟踪(即使这是一个bug,如链接问题中所述)。MBR回答说,关闭绘图应该可以解决您的问题


另一种方法,正如您链接的问题所解释的,是保留所创建的标题的引用。然后,由于它们表现为
文本
对象,您可以更改它们的特征,尤其是显示的文本。

顺便说一句,您可以使用LaTeX格式改进标签的外观,将
中的“Mm^3”
替换为
r“Mm$^3$”
。谢谢您的提示。。