Dynamically plot, show, and close changing data sizes in python

时间:2016-07-11 21:29:27

标签: python matplotlib plot

I need to plot, and show a matplotlib graph that updates dynamically. Problem is, my data size is always growing by one. I'm ok replotting the whole thing, but I need the plot to be visible until the next one is available. As far as I can tell, because my data shape is changing, I can't use plt.ion()

Here's what I have.

import numpy as np
from matplotlib import pyplot as plt


while inputs != outputs:
    percent_complete = inputs/outputs      #Total number of input files completed
    y = np.append(y, percent_complete)
    plt.plot(np.arange(len(y)), y)
    time.sleep(10)
    update(inputs)               #This function counts the number of inputs have complete
    plt.close()

So the problem with this is that the entire program waits for you to manually close the plot before continuing. Is there a way to visualize the plot, continue running the program, close the plot and open a new updated one?

Also, I needs to work across platform. I thought about adding an os open file statement, but that would need to be os specific.

1 个答案:

答案 0 :(得分:0)

I was able to figure this out, due to help from this answer, though I am still getting a deprecated matplotlib answer.

import numpy as np
from matplotlib import pyplot as plt

plt.ion()
while inputs != outputs:
    percent_complete = inputs/outputs      #Total number of input files completed
    y = np.append(y, percent_complete)
    plt.plot(np.arange(len(y)), y)
    plt.pause(.001)
    time.sleep(10)
    update(inputs)               #This function counts the number of inputs have complete
    plt.close()

Not sure I understand why adding in the plt.pause(.001) solves this, but it works.