Skip to content

Commit c5ea558

Browse files
committed
twinx and twiny inherit autoscale setting for shared axis from parent Axes
1 parent 03c1eb5 commit c5ea558

File tree

3 files changed

+55
-17
lines changed

3 files changed

+55
-17
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3902,18 +3902,18 @@ def _make_twin_axes(self, *kl, **kwargs):
39023902

39033903
def twinx(self):
39043904
"""
3905-
Create a twin Axes sharing the xaxis
3905+
Create a twin Axes instance sharing the x-axis with self
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
3911-
`~matplotlib.ticker.LinearLocator`
3907+
Create a twin Axes instance for generating a plot with a shared
3908+
x-axis but independent y-axis. The y-ticks on the returned Axes will
3909+
be on the right, opposite those of the original Axes. The x-axis
3910+
autoscale setting will be inherited from the parent Axes. To ensure
3911+
tick marks of both axis align, see `~matplotlib.ticker.LinearLocator`
39123912
39133913
Returns
39143914
-------
3915-
Axis
3916-
The newly created axis
3915+
Axes
3916+
The newly created Axes instance
39173917
39183918
Notes
39193919
-----
@@ -3924,34 +3924,36 @@ 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)
39303931
return ax2
39313932

39323933
def twiny(self):
39333934
"""
3934-
Create a twin Axes sharing the yaxis
3935+
Create a twin Axes instance sharing the y-axis with self
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 twin Axes instance for generating a plot with a shared
3938+
y-axis but independent x-axis. The x-ticks on the returned Axes will
3939+
be on the top, opposite those of the original Axes. The y-axis
3940+
autoscale setting will be inherited from the parent Axes. To ensure
3941+
tick marks of both axis align, see `~matplotlib.ticker.LinearLocator`
39403942
39413943
Returns
39423944
-------
3943-
Axis
3944-
The newly created axis
3945+
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)
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,42 @@ def test_twinx_cla():
145145
assert ax.yaxis.get_visible()
146146

147147

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

0 commit comments

Comments
 (0)