Updating a plot in python's matplotlib

旧城冷巷雨未停 提交于 2019-12-11 17:58:31

问题


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?


回答1:


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?



来源:https://stackoverflow.com/questions/36863607/updating-a-plot-in-pythons-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!