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

Skip to content

Commit 9a54497

Browse files
authored
Merge pull request #18459 from meeseeksmachine/auto-backport-of-pr-18393-on-v3.3.x
Backport PR #18393 on branch v3.3.x (Fix Axis scale on twinned Axes.)
2 parents 7776ef5 + a4f4d71 commit 9a54497

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,8 @@ def __init__(self, fig, rect,
490490
self._anchor = 'C'
491491
self._stale_viewlim_x = False
492492
self._stale_viewlim_y = False
493-
self._sharex = None
494-
self._sharey = None
493+
self._sharex = sharex
494+
self._sharey = sharey
495495
self.set_label(label)
496496
self.set_figure(fig)
497497
self.set_box_aspect(box_aspect)
@@ -510,11 +510,6 @@ def __init__(self, fig, rect,
510510
self._rasterization_zorder = None
511511
self.cla()
512512

513-
if sharex is not None:
514-
self.sharex(sharex)
515-
if sharey is not None:
516-
self.sharey(sharey)
517-
518513
# funcs used to format x and y - fall back on major formatters
519514
self.fmt_xdata = None
520515
self.fmt_ydata = None

lib/matplotlib/tests/test_axes.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,50 @@ def test_twinx_cla():
305305
assert ax.yaxis.get_visible()
306306

307307

308+
@pytest.mark.parametrize('twin', ('x', 'y'))
309+
@check_figures_equal(extensions=['png'], tol=0.19)
310+
def test_twin_logscale(fig_test, fig_ref, twin):
311+
twin_func = f'twin{twin}' # test twinx or twiny
312+
set_scale = f'set_{twin}scale'
313+
x = np.arange(1, 100)
314+
315+
# Change scale after twinning.
316+
ax_test = fig_test.add_subplot(2, 1, 1)
317+
ax_twin = getattr(ax_test, twin_func)()
318+
getattr(ax_test, set_scale)('log')
319+
ax_twin.plot(x, x)
320+
321+
# Twin after changing scale.
322+
ax_test = fig_test.add_subplot(2, 1, 2)
323+
getattr(ax_test, set_scale)('log')
324+
ax_twin = getattr(ax_test, twin_func)()
325+
ax_twin.plot(x, x)
326+
327+
for i in [1, 2]:
328+
ax_ref = fig_ref.add_subplot(2, 1, i)
329+
getattr(ax_ref, set_scale)('log')
330+
ax_ref.plot(x, x)
331+
332+
# This is a hack because twinned Axes double-draw the frame.
333+
# Remove this when that is fixed.
334+
Path = matplotlib.path.Path
335+
fig_ref.add_artist(
336+
matplotlib.patches.PathPatch(
337+
Path([[0, 0], [0, 1],
338+
[0, 1], [1, 1],
339+
[1, 1], [1, 0],
340+
[1, 0], [0, 0]],
341+
[Path.MOVETO, Path.LINETO] * 4),
342+
transform=ax_ref.transAxes,
343+
facecolor='none',
344+
edgecolor=mpl.rcParams['axes.edgecolor'],
345+
linewidth=mpl.rcParams['axes.linewidth'],
346+
capstyle='projecting'))
347+
348+
remove_ticks_and_titles(fig_test)
349+
remove_ticks_and_titles(fig_ref)
350+
351+
308352
@image_comparison(['twin_autoscale.png'])
309353
def test_twinx_axis_scales():
310354
x = np.array([0, 0.5, 1])

0 commit comments

Comments
 (0)