Last active
July 31, 2016 00:14
-
-
Save syrte/1d064ee9a151e2d07ae909e789084cf8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from matplotlib import pyplot as plt | |
def twin(show="xy", ax=None): | |
""" | |
Call signature:: | |
ax = twin() | |
create a twin of Axes for generating a plot with a shared | |
x-axis and y-axis. | |
The x-axis (y-axis) of ax will have ticks on bottom (left) | |
and the returned axes will have ticks on the top (right). | |
.. note:: | |
For those who are 'picking' artists while using twin, pick | |
events are only called for the artists in the top-most axes. | |
This function will be wrong when the axis-limits are changed by xticks. | |
This can be corrected by call `xlim` to reset the limits. | |
""" | |
assert show in ['x', 'y', 'xy'] | |
if ax is None: | |
ax = plt.gca() | |
ax.xaxis.tick_bottom() | |
ax.yaxis.tick_left() | |
ax2 = ax._make_twin_axes() | |
ax2._shared_x_axes.join(ax2, ax) | |
ax2._shared_y_axes.join(ax2, ax) | |
ax2._adjustable = 'datalim' | |
ax2.set_xlim(ax.get_xlim(), emit=False, auto=False) | |
ax2.set_ylim(ax.get_ylim(), emit=False, auto=False) | |
ax2.xaxis._set_scale(ax.xaxis.get_scale()) | |
ax2.yaxis._set_scale(ax.yaxis.get_scale()) | |
ax2.xaxis.tick_top() | |
ax2.yaxis.tick_right() | |
ax2.xaxis.set_label_position('top') | |
ax2.yaxis.set_label_position('right') | |
ax2.yaxis.set_offset_position('right') | |
ax2.patch.set_visible(False) | |
if show == 'x': | |
ax2.yaxis.set_visible(False) | |
elif show == 'y': | |
ax2.xaxis.set_visible(False) | |
return ax2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment