diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index cfc30d5e99ce..880e01eb9297 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1834,12 +1834,30 @@ def set_ticks(self, ticks, minor=False): """ # XXX if the user changes units, the information will be lost here ticks = self.convert_units(ticks) - if len(ticks) > 1: - xleft, xright = self.get_view_interval() - if xright > xleft: - self.set_view_interval(min(ticks), max(ticks)) - else: - self.set_view_interval(max(ticks), min(ticks)) + if self is self.axes.xaxis: + shared = [ + ax.xaxis + for ax in self.axes.get_shared_x_axes().get_siblings(self.axes) + ] + elif self is self.axes.yaxis: + shared = [ + ax.yaxis + for ax in self.axes.get_shared_y_axes().get_siblings(self.axes) + ] + elif hasattr(self.axes, "zaxis") and self is self.axes.zaxis: + shared = [ + ax.zaxis + for ax in self.axes._shared_z_axes.get_siblings(self.axes) + ] + else: + shared = [self] + for axis in shared: + if len(ticks) > 1: + xleft, xright = axis.get_view_interval() + if xright > xleft: + axis.set_view_interval(min(ticks), max(ticks)) + else: + axis.set_view_interval(max(ticks), min(ticks)) self.axes.stale = True if minor: self.set_minor_locator(mticker.FixedLocator(ticks)) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 336543329355..f9c230758833 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6779,6 +6779,21 @@ def test_2dcolor_plot(fig_test, fig_ref): axs[4].bar(np.arange(10), np.arange(10), color=color.reshape((1, -1))) +def test_shared_axes_retick(): + fig, axs = plt.subplots(2, 2, sharex='all', sharey='all') + + for ax in axs.flat: + ax.plot([0, 2], 'o-') + + axs[0, 0].set_xticks([-0.5, 0, 1, 1.5]) # should affect all axes xlims + for ax in axs.flat: + assert ax.get_xlim() == axs[0, 0].get_xlim() + + axs[0, 0].set_yticks([-0.5, 0, 2, 2.5]) # should affect all axes ylims + for ax in axs.flat: + assert ax.get_ylim() == axs[0, 0].get_ylim() + + @pytest.mark.parametrize('ha', ['left', 'center', 'right']) def test_ylabel_ha_with_position(ha): fig = Figure() diff --git a/lib/mpl_toolkits/tests/test_mplot3d.py b/lib/mpl_toolkits/tests/test_mplot3d.py index d1f690b678f0..3c240c1e0127 100644 --- a/lib/mpl_toolkits/tests/test_mplot3d.py +++ b/lib/mpl_toolkits/tests/test_mplot3d.py @@ -1254,6 +1254,18 @@ def test_colorbar_pos(): assert cbar.ax.get_position().extents[1] < 0.2 +def test_shared_axes_retick(): + fig = plt.figure() + ax1 = fig.add_subplot(211, projection="3d") + ax2 = fig.add_subplot(212, projection="3d", sharez=ax1) + ax1.plot([0, 1], [0, 1], [0, 2]) + ax2.plot([0, 1], [0, 1], [0, 2]) + ax1.set_zticks([-0.5, 0, 2, 2.5]) + # check that setting ticks on a shared axis is synchronized + assert ax1.get_zlim() == (-0.5, 2.5) + assert ax2.get_zlim() == (-0.5, 2.5) + + def test_pan(): """Test mouse panning using the middle mouse button."""