Skip to content

Commit d8dda52

Browse files
committed
Merge pull request matplotlib#1169 from leejjoon/fix-subplot-twinxy
Subplot.twin[xy] returns a Subplot instance
2 parents 9448ff2 + 14e34cc commit d8dda52

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

doc/api/api_changes.rst

+4
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ Changes in 1.2.x
120120
``ax.transData + ax.transAxes.inverted()`` (depth is a new concept, but had it existed
121121
it would return 4 for this example).
122122

123+
* ``twinx`` and ``twiny`` now returns an instance of SubplotBase if
124+
parent axes is an instance of SubplotBase.
125+
126+
123127
Changes in 1.1.x
124128
================
125129

lib/matplotlib/axes.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -7677,6 +7677,14 @@ def table(self, **kwargs):
76777677
"""
76787678
return mtable.table(self, **kwargs)
76797679

7680+
def _make_twin_axes(self, *kl, **kwargs):
7681+
"""
7682+
make a twinx axes of self. This is used for twinx and twiny.
7683+
"""
7684+
ax2 = self.figure.add_axes(self.get_position(True), *kl, **kwargs)
7685+
return ax2
7686+
7687+
76807688
def twinx(self):
76817689
"""
76827690
Call signature::
@@ -7693,8 +7701,7 @@ def twinx(self):
76937701
events are only called for the artists in the top-most axes.
76947702
"""
76957703

7696-
ax2 = self.figure.add_axes(self.get_position(True), sharex=self,
7697-
frameon=False)
7704+
ax2 = self._make_twin_axes(sharex=self, frameon=False)
76987705
ax2.yaxis.tick_right()
76997706
ax2.yaxis.set_label_position('right')
77007707
ax2.yaxis.set_offset_position('right')
@@ -7718,8 +7725,7 @@ def twiny(self):
77187725
events are only called for the artists in the top-most axes.
77197726
"""
77207727

7721-
ax2 = self.figure.add_axes(self.get_position(True), sharey=self,
7722-
frameon=False)
7728+
ax2 = self._make_twin_axes(sharey=self, frameon=False)
77237729
ax2.xaxis.tick_top()
77247730
ax2.xaxis.set_label_position('top')
77257731
self.xaxis.tick_bottom()
@@ -8899,6 +8905,13 @@ def label_outer(self):
88998905
label.set_visible(firstcol)
89008906

89018907

8908+
def _make_twin_axes(self, *kl, **kwargs):
8909+
"""
8910+
make a twinx axes of self. This is used for twinx and twiny.
8911+
"""
8912+
ax2 = self.figure.add_subplot(self.get_subplotspec(), *kl, **kwargs)
8913+
return ax2
8914+
89028915

89038916
_subplot_classes = {}
89048917
def subplot_class_factory(axes_class=None):

0 commit comments

Comments
 (0)