Open and close a plot within a while

2

I'm trying to plot a signal with Matplotlib and the plot is within while . What I want is that every time I go through the while a graph is opened and then either immediately or after a time that I can control it will close. I have something more or less like that, but it does not work.

import matplotlib.pyplot as plt
from Ciclo import ciclo

b = ciclo()

g = []

pin = 1

while (True):

    val = b.cicloHigh(pin)
    t = t + 1
    res = int((int(val)*250000)/1023)
    g.append(res)
    print("%s ............... %s"%(res, t))
    plt.figure(1)
    plt.plot(g)
    plt.show()
    plt.close()
    time.sleep(0.1) 

Because plt.show() is a cycle then it never closes unless it is manual.

And I want it to open and close once or if it is possible to close at a certain time, but I do not achieve either of the two.

    
asked by Michael Lan 22.04.2016 в 10:19
source

2 answers

0

If I understand correctly, what you say is that the program stops in plt.show() and what you want is to continue with the next iteration. For this you can pass an argument telling him not to block:

plt.show(block=False)

If you do not solve the problem, add some more explanation of what you want.

    
answered by 22.04.2016 / 10:46
source
4

Complementing what you have written @ChemaCortes, you are missing something else. For doing it in a simple way, I have included comments in an example below:

import numpy as np
import matplotlib.pyplot as plt
from time import sleep

y = np.random.randn(10)

while True: # Bucle infinito que puedes cerrar con crtl+c
    plt.figure(1)
    plt.plot(y)
    plt.show(block = False) # Para que no se congele la ejecución
    sleep(2) # La imagen se mostrará 2 segundos.
    plt.close(1)  # Es importante que indiques qué vas a cerrar
                  # En plt.figure he usado 1 para darle un índice a la figura
                  # En plt.close indico ese índice para saber qué hay que cerrar
    sleep(1) # esperamos 1 segundo para generar la nueva imagen.

Greetings.

    
answered by 22.04.2016 в 12:52