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

Skip to content

Commit c8203fb

Browse files
authored
Merge pull request #18529 from l-johnston/issue_8946
Synchronize view limits of shared axes after setting ticks
2 parents 0ef97df + 5a1a82c commit c8203fb

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

lib/matplotlib/axis.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,12 +1850,30 @@ def set_ticks(self, ticks, minor=False):
18501850
"""
18511851
# XXX if the user changes units, the information will be lost here
18521852
ticks = self.convert_units(ticks)
1853-
if len(ticks) > 1:
1854-
xleft, xright = self.get_view_interval()
1855-
if xright > xleft:
1856-
self.set_view_interval(min(ticks), max(ticks))
1857-
else:
1858-
self.set_view_interval(max(ticks), min(ticks))
1853+
if self is self.axes.xaxis:
1854+
shared = [
1855+
ax.xaxis
1856+
for ax in self.axes.get_shared_x_axes().get_siblings(self.axes)
1857+
]
1858+
elif self is self.axes.yaxis:
1859+
shared = [
1860+
ax.yaxis
1861+
for ax in self.axes.get_shared_y_axes().get_siblings(self.axes)
1862+
]
1863+
elif hasattr(self.axes, "zaxis") and self is self.axes.zaxis:
1864+
shared = [
1865+
ax.zaxis
1866+
for ax in self.axes._shared_z_axes.get_siblings(self.axes)
1867+
]
1868+
else:
1869+
shared = [self]
1870+
for axis in shared:
1871+
if len(ticks) > 1:
1872+
xleft, xright = axis.get_view_interval()
1873+
if xright > xleft:
1874+
axis.set_view_interval(min(ticks), max(ticks))
1875+
else:
1876+
axis.set_view_interval(max(ticks), min(ticks))
18591877
self.axes.stale = True
18601878
if minor:
18611879
self.set_minor_locator(mticker.FixedLocator(ticks))

lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6792,6 +6792,21 @@ def test_2dcolor_plot(fig_test, fig_ref):
67926792
axs[4].bar(np.arange(10), np.arange(10), color=color.reshape((1, -1)))
67936793

67946794

6795+
def test_shared_axes_retick():
6796+
fig, axs = plt.subplots(2, 2, sharex='all', sharey='all')
6797+
6798+
for ax in axs.flat:
6799+
ax.plot([0, 2], 'o-')
6800+
6801+
axs[0, 0].set_xticks([-0.5, 0, 1, 1.5]) # should affect all axes xlims
6802+
for ax in axs.flat:
6803+
assert ax.get_xlim() == axs[0, 0].get_xlim()
6804+
6805+
axs[0, 0].set_yticks([-0.5, 0, 2, 2.5]) # should affect all axes ylims
6806+
for ax in axs.flat:
6807+
assert ax.get_ylim() == axs[0, 0].get_ylim()
6808+
6809+
67956810
@pytest.mark.parametrize('ha', ['left', 'center', 'right'])
67966811
def test_ylabel_ha_with_position(ha):
67976812
fig = Figure()

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,18 @@ def test_colorbar_pos():
12541254
assert cbar.ax.get_position().extents[1] < 0.2
12551255

12561256

1257+
def test_shared_axes_retick():
1258+
fig = plt.figure()
1259+
ax1 = fig.add_subplot(211, projection="3d")
1260+
ax2 = fig.add_subplot(212, projection="3d", sharez=ax1)
1261+
ax1.plot([0, 1], [0, 1], [0, 2])
1262+
ax2.plot([0, 1], [0, 1], [0, 2])
1263+
ax1.set_zticks([-0.5, 0, 2, 2.5])
1264+
# check that setting ticks on a shared axis is synchronized
1265+
assert ax1.get_zlim() == (-0.5, 2.5)
1266+
assert ax2.get_zlim() == (-0.5, 2.5)
1267+
1268+
12571269
def test_pan():
12581270
"""Test mouse panning using the middle mouse button."""
12591271

0 commit comments

Comments
 (0)