8000 Cleanups: np.clip and np.ptp are awesome by anntzer · Pull Request #7488 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Cleanups: np.clip and np.ptp are awesome #7488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 2, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify drag_zoom; clarify draw_rubberband.
  • Loading branch information
anntzer committed Jun 10, 2017
commit bad265755784a5e6c142d233c9ff67ec1708d620
22 changes: 9 additions & 13 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2780,7 +2780,10 @@ def dynamic_update(self):
self.canvas.draw_idle()

def draw_rubberband(self, event, x0, y0, x1, y1):
"""Draw a rectangle rubberband to indicate zoom limits."""
"""Draw a rectangle rubberband to indicate zoom limits.

Note that it is not guaranteed that ``x0 <= x1`` and ``y0 <= y1``.
"""

def remove_rubberband(self):
"""Remove the rubberband."""
Expand Down Expand Up @@ -3029,20 +3032,13 @@ def drag_zoom(self, event):
if self._xypress:
x, y = event.x, event.y
lastx, lasty, a, ind, view = self._xypress[0]

# adjust x, last, y, last
x1, y1, x2, y2 = a.bbox.extents
x, lastx = max(min(x, lastx), x1), min(max(x, lastx), x2)
y, lasty = max(min(y, lasty), y1), min(max(y, lasty), y2)

(x1, y1), (x2, y2) = np.clip(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is on a hot-path (mouse move event callback), might be worth micro-benchmarking this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't see any slow down when trying the different backends manually (because the drawing of the canvas is probably way slower than this, even though it's obvious the new version will be a bit slower). I can roll back this patch if you prefer...

[[lastx, lasty], [x, y]], a.bbox.min, a.bbox.max)
if self._zoom_mode == "x":
x1, y1, x2, y2 = a.bbox.extents
y, lasty = y1, y2
y1, y2 = a.bbox.intervaly
elif self._zoom_mode == "y":
x1, y1, x2, y2 = a.bbox.extents
x, lastx = x1, x2

self.draw_rubberband(event, x, y, lastx, lasty)
x1, x2 = a.bbox.intervalx
self.draw_rubberband(event, x1, y1, x2, y2)

def release_zoom(self, event):
"""Callback for mouse button release in zoom to rect mode."""
Expand Down
22 changes: 7 additions & 15 deletions lib/matplotlib/backend_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,23 +895,15 @@ def _mouse_move(self, event):

if self._xypress:
x, y = event.x, event.y
lastx, lasty, a, _ind, _view = self._xypress[0]

# adjust x, last, y, last
x1, y1, x2, y2 = a.bbox.extents
x, lastx = max(min(x, lastx), x1), min(max(x, lastx), x2)
y, lasty = max(min(y, lasty), y1), min(max(y, lasty), y2)

lastx, lasty, a, ind, view = self._xypress[0]
(x1, y1), (x2, y2) = np.clip(
[[lastx, lasty], [x, y]], a.bbox.min, a.bbox.max)
if self._zoom_mode == "x":
x1, y1, x2, y2 = a.bbox.extents C3B5
y, lasty = y1, y2
y1, y2 = a.bbox.intervaly
elif self._zoom_mode == "y":
x1, y1, x2, y2 = a.bbox.extents
x, lastx = x1, x2

self.toolmanager.trigger_tool('rubberband',
self,
data=(x, y, lastx, lasty))
x1, x2 = a.bbox.intervalx
self.toolmanager.trigger_tool(
'rubberband', self, data=(x1, y1, x2, y2))

def _release(self, event):
"""the release mouse button callback in zoom to rect mode"""
Expand Down
6 changes: 1 addition & 5 deletions lib/matplotlib/backends/backend_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,11 +684,7 @@ def draw_rubberband(self, event, x0, y0, x1, y1):
height = self.canvas.figure.bbox.height
y1 = height - y1
y0 = height - y0

w = abs(x1 - x0)
h = abs(y1 - y0)

rect = [int(val) for val in (min(x0, x1), min(y0, y1), w, h)]
rect = [int(val) for val in (x0, y0, x1 - x0, y1 - y0)]
self.canvas.drawRectangle(rect)

def remove_rubberband(self):
Expand Down
0