Search code examples
pythonmatplotlibpausegraphical-interaction

Unpause using GUI button in matplotlib (Python)


I am creating an interactive figure using matplotlib.

The data given as input is computed in real time and the function used to create the figure is called when the data is ready to be drawn, so I can not use the animation module. Apart from this, new data is not presented in the same axes as previous one and the output figure includes 3d plotting and gui widgets.

Every time data is drawn, I call plt.pause(30), if the user wants to interact with the output. When 30 seconds have passed, figures are updated.

What I am trying to do is to create a gui button that, when pressed, will cancel plt.pause and unblock the program execution. Till now I have not been able to find an answer to this simple (?) inquiry. Does anyone know if there is a way to stop plt.pause() from GUI, by binding it somehow to a gui widget? If not, is there a way to bypass this situation?


Solution

  • Ok, I had to go to github of matplotlib to understand what is the main idea. A function like the following:

    def button_function(val):
       plt.gcf().canvas.stop_event_loop()
    

    does exactly what I want when connected with a button using on_clicked. This question puzzled me for one day and when I posted the question I found the answer under 10 minutes. Thank you stackoverflow for giving me a way to write it down....