Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Open
Labels
Description
Bug summary
I am findingmatplotlib.animation.FuncAnimation
only updates whenplt.show(block=True)
. If this is the case, I think it should at least be documented inits docs.
Neither of the following work:
plt.show(block=False)
(main thread) andplt.pause
(main thread)plt.show(block=False)
(main thread) andfig.canvas.draw()
andfig.canvas.flush_events()
(data generation thread)
Code for reproduction
importrandomimporttimefromcollections.abcimportIterablefromthreadingimportThreadimportmatplotlibimportmatplotlib.animationasanimationimportmatplotlib.artistimportmatplotlib.pyplotaspltmatplotlib.use("QtAgg")# NOTE: need interactive backend to show plotfig,ax=plt.subplots()ax.set_xlim(0,1)ax.set_ylim(0,1)lines:list[matplotlib.artist.Artist]= []defadd_line(num_points:int=10,sleep_time:float=0.2)->None:x_data,y_data= [], [] (line,)=ax.plot(x_data,y_data)lines.append(line)defadd_random_datapoint()->None:x_data.append(random.random())y_data.append(random.random())line.set_data(x_data,y_data)# NOTE: I am letting FuncAnimation handle drawing and flushing eventsfor_inrange(num_points):add_random_datapoint()time.sleep(sleep_time)defanimate(frame:int,*fargs)->Iterable[matplotlib.artist.Artist]:yieldfromlinesani=animation.FuncAnimation(fig,animate,interval=100)plt.pause(0.1)# Begin seeding datathread=Thread(target=add_line,daemon=False)thread.start()# plt.show(block=True) # Works fineplt.show(block=False)# Doesn't workthread.join()_=0
Actual outcome
The data doesn't show up on the plot until the full data generation has taken place, regardless of addingplt.pause
or canvas flushing calls.
Expected outcome
I expectanimation.FuncAnimation(fig, animate, interval=100)
to display data every 100-ms, even ifplt.show(block=False)
.
Additional information
No response
Operating system
macOS Monterey version 12.6
Matplotlib Version
3.6.2
Matplotlib Backend
QtAgg
Python version
3.10.8
Jupyter version
n/a
Installation
pip