简体   繁体   中英

Updating a plot in python's matplotlib

I'm trying to plot streaming data in matplotlib. I can update the plot using interactive mode and the set_ydata function. It animates and everything looks good until the loop ends. Then the python kernel crashes and I get this message:

C:\\Conda\\lib\\site-packages\\matplotlib\\backend_bases.py:2437: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented warnings.warn(str, mplDeprecation)

Here's the code:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 10, 0.1)
y = np.sin(x)

plt.ion() #interactive mode on
ax = plt.gca()
line, = ax.plot(x,y)
ax.set_ylim([-5,5])

for i in np.arange(100):
    line.set_ydata(y)
    plt.draw()
    y = y*1.01
    plt.pause(0.1)

Can anyone tell me why this is crashing instead of just exiting the loop? I'm doing this in Jupyter with Python 3. And of course, if there's a better way to do this, I would love to hear about it. Thanks!

This code was adapted from How to update a plot in matplotlib?

It works well for me with mac_osx backend from Jupyter notebook in python 3.4 .

Maybe you want to add plt.close() at the end to keep things tidy and prevent a hang up?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM