问题
I have a numpy array which I initialized outside the loop using np.zeros. This array is updated using some function inside a for a loop. I wish to plot the array as it changes with each iteration.
Most of the answers I have seen here are for lists and not ndarrays. I have seen the following links. Some of them I have tried to modify for my purpose but to no avail.
How to update a plot in matplotlib?
https://github.com/stsievert/python-drawnow/blob/master/drawnow/drawnow.py @Scott Sievert, I saw your code too. But unfortunately, I haven't been able to figure out how to modify it.
Real-time plotting using matplotlib and kivy in Python
Real-time plotting using matplotlib and kivy in Python
Real time trajectory plotting - Matplotlib
https://realpython.com/python-matplotlib-guide/
https://gist.github.com/vaclavcadek/66c9c61a1fac30150514a665c4bcb5dc
http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/
Matplotlib: Animate 2D Array
So basically I want to see the value of the ndarray y in real-time. (see the code below)
I am running it as a script.@Scott Staniewicz
from numpy.random import random_sample
from numpy import arange, zeros
x = arange(0, 10)
y = zeros((10, 1))
for i in range(10):
y[i] = sin(random_sample())
回答1:
The most basic version would look like
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 10)
y = np.zeros((10, 1))
fig = plt.figure()
line, = plt.plot(x,y)
plt.ylim(-0.2,1.2)
for i in range(10):
y[i] = np.sin(np.random.random_sample())
line.set_data(x[:i+1], y[:i+1])
plt.pause(1)
plt.show()
回答2:
Disclaimer : I am quite sure my answer is not the most optimal, but this is what I could achieve for now.
Modifying @Scott (Scott Sievert) answer and using his drawnow Github package I have put together this answer. I didn't install drawnow Github package . Instead I just copied drawnow.py into my folder. (This is because I didn't find any way to install it via conda. I didn't wan't to use PyPi)
from numpy.random import random_sample
from numpy import arange, zeros
from math import sin
from drawnow import drawnow
from matplotlib import use
from matplotlib.pyplot import figure, axes, ion
from matplotlib import rcParams
from matplotlib.pyplot import style
from matplotlib.pyplot import cla, close
use("TkAgg")
pgf_with_rc_fonts = {"pgf.texsystem": "pdflatex"}
rcParams.update(pgf_with_rc_fonts)
style.use('seaborn-whitegrid')
max_iter = 10**(3) # 10**(5) # 10**(2)
y = zeros((max_iter, 1))
def draw_fig():
# can be arbitrarily complex; just to draw a figure
# figure() # don't call!
scott_ax = axes()
scott_ax.plot(x, y, '-g', label='label')
# cla()
# close(scott_fig)
# show() # don't call!
scott_fig = figure() # call here instead!
ion()
# figure() # call here instead!
# ion() # enable interactivity
x = arange(0, max_iter)
for i in range(max_iter):
# cla()
# close(scott_fig)
y[i] = sin(random_sample())
drawnow(draw_fig)
来源:https://stackoverflow.com/questions/57303606/how-do-i-plot-an-updating-numpy-ndarray-in-real-time-using-matplotlib