Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Synchronize view limits of shared axes after setting ticks #18529

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 16, 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
30 changes: 24 additions & 6 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

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

It seems that maybe xleft and xright should be set outside the loop in case xleft and xright have been swapped on a shared axes somehow? I assume the idea is for all the axes to be identical.

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))
Expand Down
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 12 additions & 0 deletions lib/mpl_toolkits/tests/test_mplot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down