Skip to content

Commit 85d76be

Browse files
authored
Merge pull request #7904 from naoyak/twin-axes-autoscale
twinx / twiny inherit autoscale behavior for shared axis
2 parents 7bcaa51 + 0c0c3d6 commit 85d76be

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

lib/matplotlib/axes/_base.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -3904,16 +3904,16 @@ def twinx(self):
39043904
"""
39053905
Create a twin Axes sharing the xaxis
39063906
3907-
Create a twin of Axes for generating a plot with a shared
3908-
x-axis but independent y-axis. The y-axis of self will have
3909-
ticks on left and the returned axes will have ticks on the
3910-
right. To ensure tick marks of both axis align, see
3907+
Create a new Axes instance with an invisible x-axis and an independent
3908+
y-axis positioned opposite to the original one (i.e. at right). The
3909+
x-axis autoscale setting will be inherited from the original Axes.
3910+
To ensure that the tick marks of both y-axes align, see
39113911
`~matplotlib.ticker.LinearLocator`
39123912
39133913
Returns
39143914
-------
3915-
Axis
3916-
The newly created axis
3915+
ax_twin : Axes
3916+
The newly created Axes instance
39173917
39183918
Notes
39193919
-----
@@ -3924,6 +3924,7 @@ def twinx(self):
39243924
ax2.yaxis.tick_right()
39253925
ax2.yaxis.set_label_position('right')
39263926
ax2.yaxis.set_offset_position('right')
3927+
ax2.set_autoscalex_on(self.get_autoscalex_on())
39273928
self.yaxis.tick_left()
39283929
ax2.xaxis.set_visible(False)
39293930
ax2.patch.set_visible(False)
@@ -3933,25 +3934,26 @@ def twiny(self):
39333934
"""
39343935
Create a twin Axes sharing the yaxis
39353936
3936-
Create a twin of Axes for generating a plot with a shared
3937-
y-axis but independent x-axis. The x-axis of self will have
3938-
ticks on bottom and the returned axes will have ticks on the
3939-
top.
3937+
Create a new Axes instance with an invisible y-axis and an independent
3938+
x-axis positioned opposite to the original one (i.e. at top). The
3939+
y-axis autoscale setting will be inherited from the original Axes.
3940+
To ensure that the tick marks of both x-axes align, see
3941+
`~matplotlib.ticker.LinearLocator`
39403942
39413943
Returns
39423944
-------
3943-
Axis
3944-
The newly created axis
3945+
ax_twin : Axes
3946+
The newly created Axes instance
39453947
39463948
Notes
3947-
------
3949+
-----
39483950
For those who are 'picking' artists while using twiny, pick
39493951
events are only called for the artists in the top-most axes.
39503952
"""
3951-
39523953
ax2 = self._make_twin_axes(sharey=self)
39533954
ax2.xaxis.tick_top()
39543955
ax2.xaxis.set_label_position('top')
3956+
ax2.set_autoscaley_on(self.get_autoscaley_on())
39553957
self.xaxis.tick_bottom()
39563958
ax2.yaxis.set_visible(False)
39573959
ax2.patch.set_visible(False)

lib/matplotlib/tests/test_axes.py

+36
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,42 @@ def test_twinx_cla():
144144
assert ax.yaxis.get_visible()
145145

146146

147+
@image_comparison(baseline_images=['twin_autoscale'], extensions=['png'])
148+
def test_twinx_axis_scales():
149+
x = np.array([0, 0.5, 1])
150+
y = 0.5 * x
151+
x2 = np.array([0, 1, 2])
152+
y2 = 2 * x2
153+
154+
fig = plt.figure()
155+
ax = fig.add_axes((0, 0, 1, 1), autoscalex_on=False, autoscaley_on=False)
156+
ax.plot(x, y, color='blue', lw=10)
157+
158+
ax2 = plt.twinx(ax)
159+
ax2.plot(x2, y2, 'r--', lw=5)
160+
161+
ax.margins(0, 0)
162+
ax2.margins(0, 0)
163+
164+
165+
@cleanup
166+
def test_twin_inherit_autoscale_setting():
167+
fig, ax = plt.subplots()
168+
ax_x_on = ax.twinx()
169+
ax.set_autoscalex_on(False)
170+
ax_x_off = ax.twinx()
171+
172+
assert ax_x_on.get_autoscalex_on()
173+
assert not ax_x_off.get_autoscalex_on()
174+
175+
ax_y_on = ax.twiny()
176+
ax.set_autoscaley_on(False)
177+
ax_y_off = ax.twiny()
178+
179+
assert ax_y_on.get_autoscaley_on()
180+
assert not ax_y_off.get_autoscaley_on()
181+
182+
147183
@image_comparison(baseline_images=["minorticks_on_rcParams_both"], extensions=['png'])
148184
def test_minorticks_on_rcParams_both():
149185
fig = plt.figure()

0 commit comments

Comments
 (0)