Skip to content

Commit

Permalink
Merge pull request #1169 from leejjoon/fix-subplot-twinxy
Browse files Browse the repository at this point in the history
Subplot.twin[xy] returns a Subplot instance
  • Loading branch information
efiring committed Sep 2, 2012
2 parents 9448ff2 + 14e34cc commit d8dda52
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions doc/api/api_changes.rst
Expand Up @@ -120,6 +120,10 @@ Changes in 1.2.x
``ax.transData + ax.transAxes.inverted()`` (depth is a new concept, but had it existed
it would return 4 for this example).

* ``twinx`` and ``twiny`` now returns an instance of SubplotBase if
parent axes is an instance of SubplotBase.


Changes in 1.1.x
================

Expand Down
21 changes: 17 additions & 4 deletions lib/matplotlib/axes.py
Expand Up @@ -7677,6 +7677,14 @@ def table(self, **kwargs):
"""
return mtable.table(self, **kwargs)

def _make_twin_axes(self, *kl, **kwargs):
"""
make a twinx axes of self. This is used for twinx and twiny.
"""
ax2 = self.figure.add_axes(self.get_position(True), *kl, **kwargs)
return ax2


def twinx(self):
"""
Call signature::
Expand All @@ -7693,8 +7701,7 @@ def twinx(self):
events are only called for the artists in the top-most axes.
"""

ax2 = self.figure.add_axes(self.get_position(True), sharex=self,
frameon=False)
ax2 = self._make_twin_axes(sharex=self, frameon=False)
ax2.yaxis.tick_right()
ax2.yaxis.set_label_position('right')
ax2.yaxis.set_offset_position('right')
Expand All @@ -7718,8 +7725,7 @@ def twiny(self):
events are only called for the artists in the top-most axes.
"""

ax2 = self.figure.add_axes(self.get_position(True), sharey=self,
frameon=False)
ax2 = self._make_twin_axes(sharey=self, frameon=False)
ax2.xaxis.tick_top()
ax2.xaxis.set_label_position('top')
self.xaxis.tick_bottom()
Expand Down Expand Up @@ -8899,6 +8905,13 @@ def label_outer(self):
label.set_visible(firstcol)


def _make_twin_axes(self, *kl, **kwargs):
"""
make a twinx axes of self. This is used for twinx and twiny.
"""
ax2 = self.figure.add_subplot(self.get_subplotspec(), *kl, **kwargs)
return ax2


_subplot_classes = {}
def subplot_class_factory(axes_class=None):
Expand Down

0 comments on commit d8dda52

Please sign in to comment.