python - Matplotlib pyplot in real time -
i have while function generates 2 lists of numbers , @ end plot them using matplotlib.pyplot.
i'm doing
while true: #.... plt.plot(list1) plt.plot(list2) plt.show()
but in order see progression have close plot window. there way refresh new info every x seconds?
the robust way want utilize matplotlib.animation
. here's illustration of animating 2 lines, 1 representing sine , 1 representing cosine.
import numpy np import matplotlib.pyplot plt import matplotlib.animation animation fig, ax = plt.subplots() sin_l, = ax.plot(np.sin(0)) cos_l, = ax.plot(np.cos(0)) ax.set_ylim(-1, 1) ax.set_xlim(0, 5) dx = 0.1 def update(i): # counter each frame. # we'll increment x dx each frame. x = np.arange(0, i) * dx sin_l.set_data(x, np.sin(x)) cos_l.set_data(x, np.cos(x)) homecoming sin_l, cos_l ani = animation.funcanimation(fig, update, frames=51, interval=50) plt.show()
for particular example, rid of while true
, set logic within while
loop in update
function. then, have create sure set_data
instead of making whole new plt.plot
call.
more details can found in this nice blog post, the animation
api, or the animation
examples.
python graph matplotlib plot
No comments:
Post a Comment