[matplotlib]:在Jupyter%matplotlib笔记本上使用ginput

时间:2016-12-30 22:21:34

标签: python matplotlib jupyter-notebook spyder

我想使用matplotlib方法" ginput"在Jupyter上,用#34;%matplotlib笔记本"但它不起作用。在这里,您可以看到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: 

你建议我做什么?&#34; ginput&#34;在Jupyter上工作的方法?

1 个答案:

答案 0 :(得分:0)

由于我只是在玩相同的示例,起初无法使其正常工作(尽管存在不同的问题,但从未收到该特定的错误消息),所以这终于对我有用:

添加

import matplotlib
matplotlib.use('TkAgg')

之前

import matplotlib.pyplot as plt

然后在一个单独的窗口中打开该图,该窗口在交互结束时关闭。

由于我想在更长的脚本中进行最终的绘图交互,因此我希望所有其他绘图以内联方式显示,但是交互式的绘图必须在单独的窗口中打开,所以我的脚本现在具有以下格式:

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
%matplotlib inline
# some plots
%matplotlib qt
# the interactive plot
%matplotlib inline
# the other plots

我尝试了很多我在网上找到的东西,但是只有通过这种组合,它才可以满足我的要求;)