[Matplotlib-users] selecting points inside plot

Jean-Philippe Grivet jean-philippe.grivet at wanadoo.fr
Fri Sep 8 11:41:46 EDT 2017


Thank you Jody,
I'll try work my way starting with your ode.
Jean-Philippe


> OK, I think you want the equivalent of matlab’s
>
> |plot(something) x,y = ginput(‘Click the figure’) plot(x, y) |
>
> /and/ you want it to execute in an interactive session in |ipython| 
> (spyder)
>
> I don’t think you can do that, though there may be some new widgets 
> that will let it happen these days.
>
> Historically, it is better to think of any graphical interaction as a 
> separate program. If you are willing to do that, then plotting the 
> data as you click is very simple:
>
> |import matplotlib matplotlib.use('Qt5Agg') import matplotlib.pyplot as 
> plt import numpy as np class Picker(object): def __init__(self, 
> ax=None): self.x = np.array([]) self.y = np.array([]) self.ax = ax 
> self.dots, = ax.plot(1, 1, marker='o') self.dots.set_xdata(self.x) 
> self.dots.set_ydata(self.y) self.fig = self.ax.get_figure() def 
> process_key(self, event): print("Key:", event.key) def 
> process_button(self, event): print("Button:", event.x, event.y, 
> event.xdata, event.ydata, event.button) self.x = np.append(self.x, 
> event.xdata) self.y = np.append(self.y, event.ydata) 
> self.dots.set_xdata(self.x) self.dots.set_ydata(self.y) plt.draw() def 
> get_x(self): return self.x def get_y(self): return self.y fig, ax = 
> plt.subplots(1, 1) picker = Picker(ax=ax) 
> fig.canvas.mpl_connect('key_press_event', picker.process_key) 
> fig.canvas.mpl_connect('button_press_event', picker.process_button) 
> plt.show() print(picker.x) # print(picker.get_x()) # the same 
> print(picker.get_x().mean()) # returns the mean of x. 
> print(picker.get_y()) print(picker.get_y().mean(|


---
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel antivirus Avast.
https://www.avast.com/antivirus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170908/0f07132d/attachment.html>


More information about the Matplotlib-users mailing list