问题描述
我正在尝试在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:
这是代码:
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.4
中Jupyter
笔记本中的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中更新图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!