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

Skip to content

Commit 4312b06

Browse files
authored
Merge pull request #16450 from anntzer/sticky-shared
Fix interaction between sticky_edges and shared axes.
2 parents e9f3459 + 167105f commit 4312b06

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2436,14 +2436,25 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
24362436
if tight is not None:
24372437
self._tight = bool(tight)
24382438

2439-
if self.use_sticky_edges and (
2440-
(self._xmargin and scalex and self._autoscaleXon) or
2441-
(self._ymargin and scaley and self._autoscaleYon)):
2442-
stickies = [artist.sticky_edges for artist in self.get_children()]
2443-
else: # Small optimization.
2444-
stickies = []
2445-
x_stickies = np.sort([x for sticky in stickies for x in sticky.x])
2446-
y_stickies = np.sort([y for sticky in stickies for y in sticky.y])
2439+
x_stickies = y_stickies = np.array([])
2440+
if self.use_sticky_edges:
2441+
# Only iterate over axes and artists if needed. The check for
2442+
# ``hasattr(ax, "lines")`` is necessary because this can be called
2443+
# very early in the axes init process (e.g., for twin axes) when
2444+
# these attributes don't even exist yet, in which case
2445+
# `get_children` would raise an AttributeError.
2446+
if self._xmargin and scalex and self._autoscaleXon:
2447+
x_stickies = np.sort(np.concatenate([
2448+
artist.sticky_edges.x
2449+
for ax in self._shared_x_axes.get_siblings(self)
2450+
if hasattr(ax, "lines")
2451+
for artist in ax.get_children()]))
2452+
if self._ymargin and scaley and self._autoscaleYon:
2453+
y_stickies = np.sort(np.concatenate([
2454+
artist.sticky_edges.y
2455+
for ax in self._shared_y_axes.get_siblings(self)
2456+
if hasattr(ax, "lines")
2457+
for artist in ax.get_children()]))
24472458
if self.get_xscale().lower() == 'log':
24482459
x_stickies = x_stickies[x_stickies > 0]
24492460
if self.get_yscale().lower() == 'log':

lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,21 @@ def test_use_sticky_edges():
403403
assert_allclose(ax.get_ylim(), (-0.5, 1.5))
404404

405405

406+
@check_figures_equal(extensions=["png"])
407+
def test_sticky_shared_axes(fig_test, fig_ref):
408+
# Check that sticky edges work whether they are set in an axes that is a
409+
# "master" in a share, or an axes that is a "follower".
410+
Z = np.arange(15).reshape(3, 5)
411+
412+
ax0 = fig_test.add_subplot(211)
413+
ax1 = fig_test.add_subplot(212, sharex=ax0)
414+
ax1.pcolormesh(Z)
415+
416+
ax0 = fig_ref.add_subplot(212)
417+
ax1 = fig_ref.add_subplot(211, sharex=ax0)
418+
ax0.pcolormesh(Z)
419+
420+
406421
@image_comparison(['offset_points'], remove_text=True)
407422
def test_basic_annotate():
408423
# Setup some data

0 commit comments

Comments
 (0)