Home > Software engineering >  How can I close the Matplotlib window without closing the whole program?
How can I close the Matplotlib window without closing the whole program?

Time:02-03

I'm using Matplotlib to plot some values I get from an ADC, and the plotting itself works fine.

I will do 5 updates then stop the plotting of the values, but I can't close the figure window without stopping the whole program (using tkinter as well).

The best option in my mind is to being able to close the window on the little X in the corner or that it closes itself after the 5 updates, or anything else. The whole execution stops whenever I press the x, I still want the tkinter to run and being able to run it again without restarting the execution.

The code can be shown below, voltage_converter and adc_1 is the other methods but they work fine. The tkinter menu I've created has a dedicated button to start sensor().

def sensor():
    plt.ion()
    fig = plt.figure(figsize=(20,5))
    ax = fig.add_subplot(1,1,1)
    lista = []
    current = []
    counter = 0
    while True: 
        lista.append(voltage_converter(adc_1()))
        for i in range(len(lista))
            plt.plot(lista[i])
            xcorr = [0,100]
            ycorr = [1.5, 1.5]
            plt.plot(xcorr, ycorr)
            plt.pause(1)
            plt.clf()
            counter = counter   1
            time.sleep(7)
            lista.clear()
        if counter == 5:
            plt.clf()
            plt.close(fig)
            break

CodePudding user response:

i assume that plt.ion() means matplotlib.pyplot.ion()?

have you tried telling the window to close itself? At the moment you are just clearing the window with plt.clf() try: matplotlib.pyplot.close()

CodePudding user response:

plt.close() should work here.

You can check link for more options of close.

  •  Tags:  
  • Related