简体   繁体   中英

[matplotlib]: use of ginput on Jupyter %matplotlib notebook

I would like to use the matplotlib method "ginput" on Jupyter, with "%matplotlib notebook" but it doesn't work. Here you see the ginput example . If I copy and past that code on Spyder then it works, but if I paste it on Jupyter then I get the following message of error

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: 

what do you suggest me to make the "ginput" method working on Jupyter?

Since I was just playing with the same example and at first couldn't get it to work (with different problems though, never got that specific error message), here is what finally worked for me:

Adding

import matplotlib
matplotlib.use('TkAgg')

before the

import matplotlib.pyplot as plt

Then the plot is opened in a separate window which closes at the end of the interaction.

Since I wanted to have the final plot interaction in a longer script, where I want all other plot to be shown inline but the interactive one had to be opened in a separate window, my script now has the format:

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

I tried a lot of different things I found online, but only in this combination it does what I want ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM