diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 94b3f3b255b8..0ea95aed250a 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3904,16 +3904,16 @@ def twinx(self): """ Create a twin Axes sharing the xaxis - Create a twin of Axes for generating a plot with a shared - x-axis but independent y-axis. The y-axis of self will have - ticks on left and the returned axes will have ticks on the - right. To ensure tick marks of both axis align, see + Create a new Axes instance with an invisible x-axis and an independent + y-axis positioned opposite to the original one (i.e. at right). The + x-axis autoscale setting will be inherited from the original Axes. + To ensure that the tick marks of both y-axes align, see `~matplotlib.ticker.LinearLocator` Returns ------- - Axis - The newly created axis + ax_twin : Axes + The newly created Axes instance Notes ----- @@ -3924,6 +3924,7 @@ def twinx(self): ax2.yaxis.tick_right() ax2.yaxis.set_label_position('right') ax2.yaxis.set_offset_position('right') + ax2.set_autoscalex_on(self.get_autoscalex_on()) self.yaxis.tick_left() ax2.xaxis.set_visible(False) ax2.patch.set_visible(False) @@ -3933,25 +3934,26 @@ def twiny(self): """ Create a twin Axes sharing the yaxis - Create a twin of Axes for generating a plot with a shared - y-axis but independent x-axis. The x-axis of self will have - ticks on bottom and the returned axes will have ticks on the - top. + Create a new Axes instance with an invisible y-axis and an independent + x-axis positioned opposite to the original one (i.e. at top). The + y-axis autoscale setting will be inherited from the original Axes. + To ensure that the tick marks of both x-axes align, see + `~matplotlib.ticker.LinearLocator` Returns ------- - Axis - The newly created axis + ax_twin : Axes + The newly created Axes instance Notes - ------ + ----- For those who are 'picking' artists while using twiny, pick events are only called for the artists in the top-most axes. """ - ax2 = self._make_twin_axes(sharey=self) ax2.xaxis.tick_top() ax2.xaxis.set_label_position('top') + ax2.set_autoscaley_on(self.get_autoscaley_on()) self.xaxis.tick_bottom() ax2.yaxis.set_visible(False) ax2.patch.set_visible(False) diff --git a/lib/matplotlib/tests/baseline_images/test_axes/twin_autoscale.png b/lib/matplotlib/tests/baseline_images/test_axes/twin_autoscale.png new file mode 100644 index 000000000000..922eeff5c43d Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/twin_autoscale.png differ diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index dacbf6987423..57fd58040e69 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -145,6 +145,42 @@ def test_twinx_cla(): assert ax.yaxis.get_visible() +@image_comparison(baseline_images=['twin_autoscale'], extensions=['png']) +def test_twinx_axis_scales(): + x = np.array([0, 0.5, 1]) + y = 0.5 * x + x2 = np.array([0, 1, 2]) + y2 = 2 * x2 + + fig = plt.figure() + ax = fig.add_axes((0, 0, 1, 1), autoscalex_on=False, autoscaley_on=False) + ax.plot(x, y, color='blue', lw=10) + + ax2 = plt.twinx(ax) + ax2.plot(x2, y2, 'r--', lw=5) + + ax.margins(0, 0) + ax2.margins(0, 0) + + +@cleanup +def test_twin_inherit_autoscale_setting(): + fig, ax = plt.subplots() + ax_x_on = ax.twinx() + ax.set_autoscalex_on(False) + ax_x_off = ax.twinx() + + assert ax_x_on.get_autoscalex_on() + assert not ax_x_off.get_autoscalex_on() + + ax_y_on = ax.twiny() + ax.set_autoscaley_on(False) + ax_y_off = ax.twiny() + + assert ax_y_on.get_autoscaley_on() + assert not ax_y_off.get_autoscaley_on() + + @image_comparison(baseline_images=["minorticks_on_rcParams_both"], extensions=['png']) def test_minorticks_on_rcParams_both(): fig = plt.figure()