1

I am working on a python script where I have the following method in an object 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()

The python script is called by a real time C++ app whenever it needs to plot something (that is why i am using plt.ion() so that the plot runs in a different thread and does not stop the c++ app) However, sometimes the c++ app needs to refresh the app and calls the following method:

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

This method effectively close the matplotlib window where I plotted the angle. However, when it calls a second time the method plot (defined above), nothing gets plotted (an empty window appears).

It seems that calling plt.close() affects the all behaviour of matplotlib (I tried to close manually the window and the script is able to plot different graphs one after the other)

Have you ever encountered this kind of issue ?

Thanks a lot for your help

Best

Vincent

4

1 回答 1

0

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

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

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

感谢您的见解!

文森特

于 2013-06-14T12:08:07.953 回答