在matplotlib中关闭第一个图之后绘制新图

时间:2013-06-13 15:54:41

标签: python matplotlib

我正在编写一个python脚本,我在对象Plotter中有以下方法:

import matplotlib.pyplot as plt
class Plotter:

   def __init__(self):
       self.nbfig = 0

   def plot(self):
       self.nbfig += 1
       plt.figure(self.nbfig)
       plt.plot(self.time, self.angle, 'b')
       plt.ion()
       plt.show()

python脚本由实时C ++应用程序调用,只要它需要绘制一些东西(这就是我使用plt.ion()的原因,以便绘图在不同的线程中运行并且不会停止c ++应用程序) 但是,有时c ++应用程序需要刷新应用程序并调用以下方法:

def refresh(self):
    if (self.nbfig > 0): #meaning the c++ app already plotted a figure
        plt.close()

这种方法有效地关闭了matplotlib窗口,在那里我绘制了角度。但是,当它第二次调用方法图(如上所述)时,不会绘制任何内容(显示一个空窗口)。

看来调用plt.close()会影响matplotlib的所有行为(我试图手动关闭窗口,脚本可以一个接一个地绘制不同的图形)

你遇到过这种问题吗?

非常感谢你的帮助

最佳

文森特

1 个答案:

答案 0 :(得分:0)

我通过添加一行代码解决了我的问题所以我想分享我的解决方案,以防有人感兴趣。 问题来自交互模式导致奇怪的行为所以在关闭窗口之前,我们需要关闭交互模式。代码现在看起来像这样:

def refresh(self):
    if (self.nbfig > 0): #meaning the c++ app already plotted a figure
        plt.ioff()
        plt.close()

现在我的脚本能够关闭一个窗口图并在之后绘制另一个。

感谢您的见解!

文森特