8000 Merge pull request #16465 from meeseeksmachine/auto-backport-of-pr-16… · matplotlib/matplotlib@bf85210 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf85210

Browse files
authored
Merge pull request #16465 from meeseeksmachine/auto-backport-of-pr-16450-on-v3.2.x
Backport PR #16450 on branch v3.2.x (Fix interaction between sticky_edges and shared axes.)
2 parents 3b38089 + 7aa4a1b commit bf85210

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
@@ -2371,14 +2371,25 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
23712371
if tight is not None:
23722372
self._tight = bool(tight)
23732373

2374-
if self.use_sticky_edges and (
2375-
(self._xmargin and scalex and self._autoscaleXon) or
2376-
(self._ymargin and scaley and self._autoscaleYon)):
2377-
stickies = [artist.sticky_edges for artist in self.get_children()]
2378-
else: # Small optimization.
2379-
stickies = []
2380-
x_stickies = np.sort([x for sticky in stickies for x in sticky.x])
2381-
y_stickies = np.sort([y for sticky in stickies for y in sticky.y])
2374+
x_stickies = y_stickies = np.array([])
2375+
if self.use_sticky_edges:
2376+
# Only iterate over axes and artists if needed. The check for
2377+
# ``hasattr(ax, "lines")`` is necessary because this can be called
2378+
# very early in the axes init process (e.g., for twin axes) when
2379+
# these attributes don't even exist yet, in which case
2380+
# `get_children` would raise an AttributeError.
2381+
if self._xmargin and scalex and self._autoscaleXon:
2382+
x_stickies = np.sort(np.concatenate([
2383+
artist.sticky_edges.x
2384+
for ax in self._shared_x_axes.get_siblings(self)
2385+
if hasattr(ax, "lines")
2386+
for artist in ax.get_children()]))
2387+
if self._ymargin and scaley and self._autoscaleYon:
2388+
y_stickies = np.sort(np.concatenate([
2389+
artist.sticky_edges.y
2390+
for ax in self._shared_y_axes.get_siblings(self)
2391+
if hasattr(ax, "lines")
2392+
for artist in ax.get_children()]))
23822393
if self.get_xscale().lower() == 'log':
23832394
x_stickies = x_stickies[x_stickies > 0]
23842395
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
@@ -334,6 +334,21 @@ def test_use_sticky_edges():
334334
assert_allclose(ax.get_ylim(), (-0.5, 1.5))
335335

336336

337+
@check_figures_equal(extensions=["png"])
338+
def test_sticky_shared_axes(fig_test, fig_ref):
339+
# Check that sticky edges work whether they are set in an axes that is a
340+
# "master" in a share, or an axes that is a "follower".
341+
Z = np.arange(15).reshape(3, 5)
342+
343+
ax0 = fig_test.add_subplot(211)
344+
ax1 = fig_test.add_subplot(212, sharex=ax0)
345+
ax1.pcolormesh(Z)
346+
347+
ax0 = fig_ref.add_subplot(212)
348+
ax1 = fig_ref.add_subplot(211, sharex=ax0)
349+
ax0.pcolormesh(Z)
350+
351+
337352
@image_comparison(['offset_points'], remove_text=True)
338353
def test_basic_annotate():
339354
# Setup some data

0 commit comments

Comments
 (0)
0