Skip to content

Commit 9ced0c0

Browse files
authored
Merge pull request matplotlib#26208 from tacaswell/fix/twin_units
FIX: also copy the axis units when creating twins
2 parents 3b3fc57 + 5570743 commit 9ced0c0

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4441,6 +4441,7 @@ def twinx(self):
44414441
self.yaxis.tick_left()
44424442
ax2.xaxis.set_visible(False)
44434443
ax2.patch.set_visible(False)
4444+
ax2.xaxis.units = self.xaxis.units
44444445
return ax2
44454446

44464447
def twiny(self):
@@ -4470,6 +4471,7 @@ def twiny(self):
44704471
self.xaxis.tick_bottom()
44714472
ax2.yaxis.set_visible(False)
44724473
ax2.patch.set_visible(False)
4474+
ax2.yaxis.units = self.yaxis.units
44734475
return ax2
44744476

44754477
def get_shared_x_axes(self):

lib/matplotlib/tests/test_axes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,23 @@ def test_twinx_cla():
376376
assert ax.yaxis.get_visible()
377377

378378

379+
@pytest.mark.parametrize('twin', ('x', 'y'))
380+
def test_twin_units(twin):
381+
axis_name = f'{twin}axis'
382+
twin_func = f'twin{twin}'
383+
384+
a = ['0', '1']
385+
b = ['a', 'b']
386+
387+
fig = Figure()
388+
ax1 = fig.subplots()
389+
ax1.plot(a, b)
390+
assert getattr(ax1, axis_name).units is not None
391+
ax2 = getattr(ax1, twin_func)()
392+
assert getattr(ax2, axis_name).units is not None
393+
assert getattr(ax2, axis_name).units is getattr(ax1, axis_name).units
394+
395+
379396
@pytest.mark.parametrize('twin', ('x', 'y'))
380397
@check_figures_equal(extensions=['png'], tol=0.19)
381398
def test_twin_logscale(fig_test, fig_ref, twin):

0 commit comments

Comments
 (0)