【问题标题】:plotting a new graph after closing a first one in matplotlib在 matplotlib 中关闭第一个图形后绘制新图形
【发布时间】:2013-06-14 20:08:07
【问题描述】:

我正在编写一个 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()

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

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

此方法有效地关闭了我绘制角度的 matplotlib 窗口。但是,当它第二次调用 plot 方法(如上定义)时,什么都没有绘制(出现一个空窗口)。

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

你遇到过这种问题吗?

非常感谢您的帮助

最好的

文森特

【问题讨论】:

  • pyplot 接口是为与ipython 一起工作而开发的,由于有关如何初始化 gui 主循环的一些细节,因此无法保证它与您的 c++ 程序配合得很好。您最好在您已经使用的任何 gui 框架中使用 OO 接口 seeembedding

标签: python matplotlib


【解决方案1】:

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

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

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

感谢您的见解!

文森特

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多