Dynamic plot works in IDLE, but not jupyter notebook

我只是一个虾纸丫 提交于 2019-12-01 12:22:42

plt.draw() doesn't work with the %matplotlib notebook, it's meant to be used in interactive mode, just as you used it in IDLE.

To overcome this there is an animation submodule which can be used.

import matplotlib.pyplot as plt
import matplotlib.animation
import time
import random
%matplotlib notebook

ysample = random.sample(range(-50, 50), 100)

xdata = []
ydata = []

axes = plt.gca()
axes.set_xlim(0, 100)
axes.set_ylim(-50, +50)
line, = axes.plot(xdata, ydata, 'r-')

def update(i):
    xdata.append(i)
    ydata.append(ysample[i])
    line.set_xdata(xdata)
    line.set_ydata(ydata)

ani= matplotlib.animation.FuncAnimation(plt.gcf(), update, frames=100,
                                       interval=100, repeat=False)

plt.show()

For more examples refer to the matplotlib page.

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