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)
def test_MultiCursor(horizOn, vertOn): fig, (ax1, ax2, ax3) = plt.subplots(3, sharex=True) # useblit=false to avoid having to draw the figure to cache the renderer multi = widgets.MultiCursor(fig.canvas, (ax1, ax2), useblit=False, horizOn=horizOn, vertOn=vertOn) # Only two of the axes should have a line drawn on them. if vertOn: assert len(multi.vlines) == 2 if horizOn: assert len(multi.hlines) == 2 # mock a motion_notify_event # Can't use `do_event` as that helper requires the widget # to have a single .ax attribute. event = mock_event(ax1, xdata=.5, ydata=.25) multi.onmove(event) # the lines in the first two ax should both move for l in multi.vlines: assert l.get_xdata() == (.5, .5) for l in multi.hlines: assert l.get_ydata() == (.25, .25) # test a move event in an axes not part of the MultiCursor # the lines in ax1 and ax2 should not have moved. event = mock_event(ax3, xdata=.75, ydata=.75) multi.onmove(event) for l in multi.vlines: assert l.get_xdata() == (.5, .5) for l in multi.hlines: assert l.get_ydata() == (.25, .25)