在python的matplotlib中更新图 [英] Updating a plot in python's matplotlib

查看:200
本文介绍了在python的matplotlib中更新图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在matplotlib中绘制流数据.我可以使用交互模式和set_ydata函数来更新绘图.它会设置动画,一切看起来都很好,直到循环结束.然后,python内核崩溃,我得到了以下消息:

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:使用默认事件循环,直到特定于函数 该GUI已实现 warnings.warn(str,mplDeprecation)

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)

这是代码:

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)

有人能告诉我为什么这会崩溃而不是退出循环吗?我正在使用Python 3在Jupyter中进行此操作.当然,如果有更好的方法可以做到这一点,我很想听听它.谢谢!

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!

此代码改编自如何在matplotlib中更新图?

推荐答案

python 3.4Jupyter笔记本中的mac_osx backend一起使用,对我来说效果很好.

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

也许您想在末尾添加plt.close()来保持整洁并防止挂断电话?

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

这篇关于在python的matplotlib中更新图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆