2

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

Vincent
  • 1,616
  • 2
  • 16
  • 18
  • The `pyplot` interface was developed to work with `ipython` and there is no guarantee that it will play nice with your c++ program due to some details about how to initialize the gui main loop. You are better off using the OO interface [see](http://stackoverflow.com/questions/14254379/how-can-i-attach-a-pyplot-function-to-a-figure-instance/14261698#14261698) and [embedding](http://matplotlib.org/examples/user_interfaces/) it in what ever gui framework you are already using. – tacaswell Jun 13 '13 at 16:27

1 Answers1

1

I solved my problem by adding just one line of code so I wanted to share my solution in case anyone is interested. The problem was coming from the interactive mode which resulted in strange behaviour so before closing the window, we need to turn off the interactive mode. The code now looks like this :

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

Now my script is able to close a window graph and plot an other one after.

Thanks for your insights!

Vincent

Vincent
  • 1,616
  • 2
  • 16
  • 18