Skip to content

Commit ead4817

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

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 16 additions & 14 deletions
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 twin Axes instance for generating a plot with a shared
3908+
x-axis and two independent y-axes on either side (left and right).
3909+
The x-axis autoscale setting will be inherited from the parent Axes. To
3910+
ensure tick marks of both axis align, see
39113911
`~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,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 twin Axes instance for generating a plot with a shared
3938+
y-axis and two independent x-axes on either side (top and bottom). The
3939+
y-axis autoscale setting will be inherited from the parent Axes. To
3940+
ensure tick marks of both axis align, see
3941+
`~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)

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)