Skip to content

axes3d panning #18536

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 4 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
Binary file added .test_pan.py.kate-swp
Binary file not shown.
4 changes: 4 additions & 0 deletions doc/users/next_whats_new/panning_3d.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Panning for mplot3d
-------------------

Click and drag with the middle mouse button to pan 3d axes.
59 changes: 43 additions & 16 deletions lib/matplotlib/testing/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,35 @@ def get_ax():
return ax


def do_event(tool, etype, button=1, xdata=0, ydata=0, key=None, step=1):
"""
Trigger an event
def mock_event(ax, button=1, xdata=0, ydata=0, key=None, step=1):
Copy link
Member

Choose a reason for hiding this comment

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

Git diff looks more complicated than it is. This just extracts the event creation out of do_event().

r"""
Create a mock event that can stand in for `.Event` and its subclasses.

This event is intended to be used in tests where it can be passed into
event handling functions.

Parameters
----------
tool : matplotlib.widgets.RectangleSelector
etype
the event to trigger
ax : `matplotlib.axes.Axes`
The axes the event will be in.
xdata : int
x coord of mouse in data coords
x coord of mouse in data coords.
ydata : int
y coord of mouse in data coords
button : int or str
button pressed None, 1, 2, 3, 'up', 'down' (up and down are used
for scroll events)
key
the key depressed when the mouse event triggered (see
:class:`KeyEvent`)
y coord of mouse in data coords.
button : None or `MouseButton` or {'up', 'down'}
The mouse button pressed in this event (see also `.MouseEvent`).
key : None or str
The key pressed when the mouse event triggered (see also `.KeyEvent`).
step : int
number of scroll steps (positive for 'up', negative for 'down')
Number of scroll steps (positive for 'up', negative for 'down').

Returns
-------
event
A `.Event`\-like Mock instance.
"""
event = mock.Mock()
event.button = button
ax = tool.ax
event.x, event.y = ax.transData.transform([(xdata, ydata),
(xdata, ydata)])[0]
event.xdata, event.ydata = xdata, ydata
Expand All @@ -52,6 +56,29 @@ def do_event(tool, etype, button=1, xdata=0, ydata=0, key=None, step=1):
event.step = step
event.guiEvent = None
event.name = 'Custom'
return event


def do_event(tool, etype, button=1, xdata=0, ydata=0, key=None, step=1):
"""
Trigger an event on the given tool.

Parameters
----------
tool : matplotlib.widgets.RectangleSelector
etype : str
The event to trigger.
xdata : int
x coord of mouse in data coords.
ydata : int
y coord of mouse in data coords.
button : None or `MouseButton` or {'up', 'down'}
The mouse button pressed in this event (see also `.MouseEvent`).
key : None or str
The key pressed when the mouse event triggered (see also `.KeyEvent`).
step : int
Number of scroll steps (positive for 'up', negative for 'down').
"""
event = mock_event(tool.ax, button, xdata, ydata, key, step)
func = getattr(tool, etype)
func(event)
18 changes: 16 additions & 2 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -1237,11 +1237,25 @@ def _on_move(self, event):
self.stale = True
self.figure.canvas.draw_idle()

# elif self.button_pressed == 2:
elif self.button_pressed == 2:
# pan view
# get the x and y pixel coords
if dx == 0 and dy == 0:
return
minx, maxx, miny, maxy, minz, maxz = self.get_w_lims()
dx = 1-((w - dx)/w)
dy = 1-((h - dy)/h)
elev, azim = np.deg2rad(self.elev), np.deg2rad(self.azim)
# project xv, yv, zv -> xw, yw, zw
dxx = (maxx-minx)*(dy*np.sin(elev)*np.cos(azim) + dx*np.sin(azim))
dyy = (maxy-miny)*(-dx*np.cos(azim) + dy*np.sin(elev)*np.sin(azim))
dzz = (maxz-minz)*(-dy*np.cos(elev))
# pan
# pass
self.set_xlim3d(minx + dxx, maxx + dxx)
self.set_ylim3d(miny + dyy, maxy + dyy)
self.set_zlim3d(minz + dzz, maxz + dzz)
self.get_proj()
self.figure.canvas.draw_idle()

# Zoom
elif self.button_pressed in self._zoom_btn:
Expand Down
40 changes: 40 additions & 0 deletions lib/mpl_toolkits/tests/test_mplot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

from mpl_toolkits.mplot3d import Axes3D, axes3d, proj3d, art3d
import matplotlib as mpl
from matplotlib.backend_bases import MouseButton
from matplotlib import cm
from matplotlib import colors as mcolors
from matplotlib.testing.decorators import image_comparison, check_figures_equal
from matplotlib.testing.widgets import mock_event
from matplotlib.collections import LineCollection, PolyCollection
from matplotlib.patches import Circle
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -1178,3 +1180,41 @@ def test_colorbar_pos():
fig.canvas.draw()
# check that actually on the bottom
assert cbar.ax.get_position().extents[1] < 0.2


def test_pan():
"""Test mouse panning using the middle mouse button."""

def convert_lim(dmin, dmax):
"""Convert min/max limits to center and range."""
center = (dmin + dmax) / 2
range_ = dmax - dmin
return center, range_

ax = plt.figure().add_subplot(projection='3d')
ax.scatter(0, 0, 0)
ax.figure.canvas.draw()

x_center0, x_range0 = convert_lim(*ax.get_xlim3d())
y_center0, y_range0 = convert_lim(*ax.get_ylim3d())
z_center0, z_range0 = convert_lim(*ax.get_zlim3d())

# move mouse diagonally to pan along all axis.
ax._button_press(
mock_event(ax, button=MouseButton.MIDDLE, xdata=0, ydata=0))
ax._on_move(
mock_event(ax, button=MouseButton.MIDDLE, xdata=1, ydata=1))

x_center, x_range = convert_lim(*ax.get_xlim3d())
y_center, y_range = convert_lim(*ax.get_ylim3d())
z_center, z_range = convert_lim(*ax.get_zlim3d())

# Ranges have not changed
assert x_range == pytest.approx(x_range0)
assert y_range == pytest.approx(y_range0)
assert z_range == pytest.approx(z_range0)

# But center positions have
assert x_center != pytest.approx(x_center0)
assert y_center != pytest.approx(y_center0)
assert z_center != pytest.approx(z_center0)