【发布时间】:2017-05-15 04:16:18
【问题描述】:
我想在 Jupyter 上使用 matplotlib 方法“ginput”和“%matplotlib notebook”,但它不起作用。在这里您可以看到 ginput example 。 如果我在 Spyder 上复制并粘贴该代码,则它可以工作,但如果我将其粘贴到 Jupyter 上,则会收到以下错误消息
Please click
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-24-50d3ac899b10> in <module>()
7 plt.plot(t, np.sin(t))
8 print("Please click")
----> 9 x = plt.ginput(3)
10 print("clicked", x)
11 plt.show()
C:\Users\fedel\Anaconda2c\lib\site-packages\matplotlib\pyplot.pyc in ginput(*args, **kwargs)
709 If *timeout* is negative, does not timeout.
710 """
--> 711 return gcf().ginput(*args, **kwargs)
712
713
C:\Users\fedel\Anaconda2c\lib\site-packages\matplotlib\figure.pyc in ginput(self, n, timeout, show_clicks, mouse_add, mouse_pop, mouse_stop)
1663 mouse_stop=mouse_stop)
1664 return blocking_mouse_input(n=n, timeout=timeout,
-> 1665 show_clicks=show_clicks)
1666
1667 def waitforbuttonpress(self, timeout=-1):
C:\Users\fedel\Anaconda2c\lib\site-packages\matplotlib\blocking_input.pyc in __call__(self, n, timeout, show_clicks)
292 self.clicks = []
293 self.marks = []
--> 294 BlockingInput.__call__(self, n=n, timeout=timeout)
295
296 return self.clicks
C:\Users\fedel\Anaconda2c\lib\site-packages\matplotlib\blocking_input.pyc in __call__(self, n, timeout)
115 try:
116 # Start event loop
--> 117 self.fig.canvas.start_event_loop(timeout=timeout)
118 finally: # Run even on exception like ctrl-c
119 # Disconnect the callbacks
C:\Users\fedel\Anaconda2c\lib\site-packages\matplotlib\backends\backend_nbagg.pyc 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\fedel\Anaconda2c\lib\site-packages\matplotlib\backend_bases.pyc in start_event_loop_default(self, timeout)
2443 self._looping = True
2444 while self._looping and counter * timestep < timeout:
-> 2445 self.flush_events()
2446 time.sleep(timestep)
2447 counter += 1
C:\Users\fedel\Anaconda2c\lib\site-packages\matplotlib\backend_bases.pyc in flush_events(self)
2388 backends with GUIs.
2389 """
-> 2390 raise NotImplementedError
2391
2392 def start_event_loop(self, timeout):
NotImplementedError:
你建议我如何让“ginput”方法在 Jupyter 上运行?
【问题讨论】:
-
正如错误类型所暗示的,
ginuput似乎尚未针对笔记本后端实现 -
我记得我可以在 Jupyter 上使用 ginput,但只能在从浏览器中绘图时使用。现在我不知道从那时起发生了什么变化,但我不能再那样做了。此外,如果我在 Juputer 上键入 plt.ginput(),我得到的是“
”。这意味着ginput在某些方面存在,但它不起作用 -
在 jupyter 中运行 ginput example code 工作正常。该问题仅在使用
%matplotlib inline或%matplotlib notebook时出现,因为显然ginput功能尚未(尚未)为这些后端实现。 -
因此问题可以用不同的方式表述:如何在 Jupyter 上的浏览器之外生成绘图?我记得我只需要输入“%matplotlib notebook”就可以做到这一点,但现在发生了一些变化,绘图永远不会离开浏览器
-
将后端设置为普通GUI(例如QT4、Tk等)
标签: python matplotlib jupyter-notebook spyder