动态绘图可在IDLE中使用,但不能在Jupyter Notebook中使用 [英] Dynamic plot works in IDLE, but not jupyter notebook
问题描述
通过空闲(Python 3.6空闲)运行时,下面的代码可以正常工作
The code below works fine when running through idle (Python 3.6 idle)
import matplotlib.pyplot as plt
import time
import random
#%matplotlib inline
ysample = random.sample(range(-50, 50), 100)
xdata = []
ydata = []
plt.show()
axes = plt.gca()
axes.set_xlim(0, 100)
axes.set_ylim(-50, +50)
line, = axes.plot(xdata, ydata, 'r-')
for i in range(100):
xdata.append(i)
ydata.append(ysample[i])
line.set_xdata(xdata)
line.set_ydata(ydata)
plt.draw()
plt.pause(1e-17)
time.sleep(0.1)
# add this if you don't want the window to disappear at the end
plt.show()
将此代码传输到jupyter笔记本时,我添加了魔术命令
When transferring this code to jupyter notebook, I'm adding the magic command
%matplotlib notebook
应该用于动态绘图.
%matplotlib notebook
which I'm told should be used for dynamic plots.
(我尝试了%matplotlib内联,但最终得到了静态图)
(I tried %matplotlib inline, but I end up with a static plot)
但是,我最终遇到了这个错误
However, I'm ending up with this error
NotImplementedError Traceback (most recent call last)
<ipython-input-7-2f88f762e22a> in <module>()
22 line.set_ydata(ydata)
23 plt.draw()
---> 24 plt.pause(1e-17)
25 time.sleep(0.1)
26
C:\Users\Moondra\AppData\Local\Programs\Python\Python36\lib\site-
packages\matplotlib\pyplot.py in pause(interval)
298 canvas.draw()
299 show(block=False)
--> 300 canvas.start_event_loop(interval)
301 return
302
C:\Users\Moondra\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backends\backend_nbagg.py in start_event_loop(self, timeout)
192
193 def start_event_loop(self, timeout):
--> 194 FigureCanvasBase.start_event_loop_default(self, timeout)
195
196 def stop_event_loop(self):
C:\Users\Moondra\AppData\Local\Programs\Python\Python36\lib\site-
packages\matplotlib\backend_bases.py in start_event_loop_default(self, timeout)
2451 self._looping = True
2452 while self._looping and counter * timestep < timeout:
-> 2453 self.flush_events()
2454 time.sleep(timestep)
2455 counter += 1
C:\Users\Moondra\AppData\Local\Programs\Python\Python36\lib\site-
packages\matplotlib\backend_bases.py in flush_events(self)
2400 backends with GUIs.
2401 """
-> 2402 raise NotImplementedError
2403
2404 def start_event_loop(self, timeout):
NotImplementedError:`
推荐答案
plt.draw()
不适用于%matplotlib notebook
,它打算以交互方式使用,就像您在IDLE中使用它一样.
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()
有关更多示例,请参见 matplotlib页面.
For more examples refer to the matplotlib page.
这篇关于动态绘图可在IDLE中使用,但不能在Jupyter Notebook中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!