From c27e807848bac0ff96d14f6be75e001225b13ece Mon Sep 17 00:00:00 2001 From: Kjell Le Date: Fri, 24 Nov 2017 03:43:44 +0100 Subject: [PATCH 1/6] Add inaxes method to figure to check whether point is in an axes. Return the top-most axes if found, else None. --- .../2017-11-24_figure_inaxes.rst | 6 +++++ lib/matplotlib/figure.py | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 doc/users/next_whats_new/2017-11-24_figure_inaxes.rst diff --git a/doc/users/next_whats_new/2017-11-24_figure_inaxes.rst b/doc/users/next_whats_new/2017-11-24_figure_inaxes.rst new file mode 100644 index 000000000000..985c910c9f9c --- /dev/null +++ b/doc/users/next_whats_new/2017-11-24_figure_inaxes.rst @@ -0,0 +1,6 @@ + +Add ``inaxes`` method to Figure +------------------------------------------------------------- + +The `Figure` class has now an ``inaxes`` method to check whether a point is in an axes +and returns the topmost axes, else None. diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index fcbad439e289..73445726f7cf 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1532,6 +1532,31 @@ def get_axes(self): """ return self.axes + def inaxes(self, xy): + """ + Check if a point is in an axes. + + Parameters + ---------- + xy : tuple or list + (x,y) coordinates. + x position - pixels from left of canvas. + y position - pixels from bottom of canvas. + + Returns + ------- + axes: topmost axes containing the point, or None if no axes. + + """ + axes_list = [a for a in self.get_axes() if a.patch.contains_point(xy)] + + if axes_list: + axes = max(reversed(axes_list), key=lambda x: x.zorder) + else: + axes = None + + return axes + @docstring.dedent_interpd def legend(self, *args, **kwargs): """ From 814212d84f000cbf69581050195f6543f0338487 Mon Sep 17 00:00:00 2001 From: Kjell Le Date: Fri, 24 Nov 2017 06:23:05 +0100 Subject: [PATCH 2/6] LocationEvent uses the new Figure.inaxes method. --- lib/matplotlib/backend_bases.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 7fec6081f405..1e971a2f1fb1 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -1496,15 +1496,9 @@ def __init__(self, name, canvas, x, y, guiEvent=None): self._update_enter_leave() return - # Find all axes containing the mouse - if self.canvas.mouse_grabber is None: - axes_list = [a for a in self.canvas.figure.get_axes() - if a.in_axes(self)] - else: - axes_list = [self.canvas.mouse_grabber] + self.inaxes = self.canvas.figure.inaxes((x, y)) - if axes_list: - self.inaxes = cbook._topmost_artist(axes_list) + if self.inaxes is not None: try: trans = self.inaxes.transData.inverted() xdata, ydata = trans.transform_point((x, y)) @@ -1514,8 +1508,6 @@ def __init__(self, name, canvas, x, y, guiEvent=None): else: self.xdata = xdata self.ydata = ydata - else: - self.inaxes = None self._update_enter_leave() From 0d4f245b40ac36a73529729b5df4dc7db04f27e8 Mon Sep 17 00:00:00 2001 From: Kjell Le Date: Fri, 24 Nov 2017 06:38:10 +0100 Subject: [PATCH 3/6] Fix LocationEvent to use default mouse_grabber for inaxes. --- lib/matplotlib/backend_bases.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 1e971a2f1fb1..831894ed4f5d 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -1496,7 +1496,10 @@ def __init__(self, name, canvas, x, y, guiEvent=None): self._update_enter_leave() return - self.inaxes = self.canvas.figure.inaxes((x, y)) + if self.canvas.mouse_grabber is None: + self.inaxes = self.canvas.figure.inaxes((x, y)) + else: + self.inaxes = self.canvas.mouse_grabber if self.inaxes is not None: try: From 1628e7cea32bca1446b7b2e49e7fba9ac714d907 Mon Sep 17 00:00:00 2001 From: Kjell Le Date: Sat, 25 Nov 2017 03:57:29 +0100 Subject: [PATCH 4/6] Use cbook._topmost_artist in Figure.inaxes. --- lib/matplotlib/figure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 73445726f7cf..b6d1d78585a2 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1551,7 +1551,7 @@ def inaxes(self, xy): axes_list = [a for a in self.get_axes() if a.patch.contains_point(xy)] if axes_list: - axes = max(reversed(axes_list), key=lambda x: x.zorder) + axes = cbook._topmost_artist(axes_list) else: axes = None From bb5ada7f68f23d2b6ed0f35133af2d851fabe62b Mon Sep 17 00:00:00 2001 From: Kjell Le Date: Sun, 26 Nov 2017 04:12:06 +0100 Subject: [PATCH 5/6] Move inaxes method from Figure to FigureCanvas --- .../2017-11-24_figure_inaxes.rst | 6 ---- .../2017-11-24_figurecanvas_inaxes.rst | 6 ++++ lib/matplotlib/backend_bases.py | 34 ++++++++++++++++++- lib/matplotlib/figure.py | 25 -------------- 4 files changed, 39 insertions(+), 32 deletions(-) delete mode 100644 doc/users/next_whats_new/2017-11-24_figure_inaxes.rst create mode 100644 doc/users/next_whats_new/2017-11-24_figurecanvas_inaxes.rst diff --git a/doc/users/next_whats_new/2017-11-24_figure_inaxes.rst b/doc/users/next_whats_new/2017-11-24_figure_inaxes.rst deleted file mode 100644 index 985c910c9f9c..000000000000 --- a/doc/users/next_whats_new/2017-11-24_figure_inaxes.rst +++ /dev/null @@ -1,6 +0,0 @@ - -Add ``inaxes`` method to Figure -------------------------------------------------------------- - -The `Figure` class has now an ``inaxes`` method to check whether a point is in an axes -and returns the topmost axes, else None. diff --git a/doc/users/next_whats_new/2017-11-24_figurecanvas_inaxes.rst b/doc/users/next_whats_new/2017-11-24_figurecanvas_inaxes.rst new file mode 100644 index 000000000000..6de6f8a7740d --- /dev/null +++ b/doc/users/next_whats_new/2017-11-24_figurecanvas_inaxes.rst @@ -0,0 +1,6 @@ + +Add ``inaxes`` method to FigureCanvas +------------------------------------------------------------- + +The `FigureCanvas` class has now an ``inaxes`` method to check whether a point is in an axes +and returns the topmost axes, else None. diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 831894ed4f5d..207c29cef71c 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -1497,7 +1497,7 @@ def __init__(self, name, canvas, x, y, guiEvent=None): return if self.canvas.mouse_grabber is None: - self.inaxes = self.canvas.figure.inaxes((x, y)) + self.inaxes = self.canvas.inaxes((x, y)) else: self.inaxes = self.canvas.mouse_grabber @@ -1988,6 +1988,38 @@ def enter_notify_event(self, guiEvent=None, xy=None): event = LocationEvent('figure_enter_event', self, x, y, guiEvent) self.callbacks.process('figure_enter_event', event) + @cbook.deprecated("2.1") + def idle_event(self, guiEvent=None): + """Called when GUI is idle.""" + s = 'idle_event' + event = IdleEvent(s, self, guiEvent=guiEvent) + self.callbacks.process(s, event) + + def inaxes(self, xy): + """ + Check if a point is in an axes. + + Parameters + ---------- + xy : tuple or list + (x,y) coordinates. + x position - pixels from left of canvas. + y position - pixels from bottom of canvas. + + Returns + ------- + axes: topmost axes containing the point, or None if no axes. + + """ + axes_list = [a for a in self.figure.get_axes() if a.patch.contains_point(xy)] + + if axes_list: + axes = cbook._topmost_artist(axes_list) + else: + axes = None + + return axes + def grab_mouse(self, ax): """ Set the child axes which are currently grabbing the mouse events. diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index b6d1d78585a2..fcbad439e289 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1532,31 +1532,6 @@ def get_axes(self): """ return self.axes - def inaxes(self, xy): - """ - Check if a point is in an axes. - - Parameters - ---------- - xy : tuple or list - (x,y) coordinates. - x position - pixels from left of canvas. - y position - pixels from bottom of canvas. - - Returns - ------- - axes: topmost axes containing the point, or None if no axes. - - """ - axes_list = [a for a in self.get_axes() if a.patch.contains_point(xy)] - - if axes_list: - axes = cbook._topmost_artist(axes_list) - else: - axes = None - - return axes - @docstring.dedent_interpd def legend(self, *args, **kwargs): """ From cb50c7a2f08b472559c0513ba5972b7b50ac0328 Mon Sep 17 00:00:00 2001 From: Kjell Le Date: Tue, 6 Mar 2018 07:50:27 +0100 Subject: [PATCH 6/6] pep8 god. --- lib/matplotlib/backend_bases.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 207c29cef71c..a8d546003158 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2011,7 +2011,8 @@ def inaxes(self, xy): axes: topmost axes containing the point, or None if no axes. """ - axes_list = [a for a in self.figure.get_axes() if a.patch.contains_point(xy)] + axes_list = [a for a in self.figure.get_axes() + if a.patch.contains_point(xy)] if axes_list: axes = cbook._topmost_artist(axes_list)