Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
ENH: Added share_tickers parameter to axes._AxesBase.twinx/y#7528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -462,6 +462,13 @@ def __init__(self, fig, rect, | ||
to share the x-axis with | ||
*sharey* an class:`~matplotlib.axes.Axes` instance | ||
to share the y-axis with | ||
*share_tickers* [ *True* | *False* ] whether the major and | ||
minor `Formatter` and `Locator` instances | ||
are always shared (if `True`) or can be set | ||
independently (if `False`) between this set | ||
of axes and `sharex` and `sharey`. This | ||
argument has no meaning if neither `sharex` | ||
nor `sharey` are set. Defaults to `True`. | ||
*title* the title string | ||
*visible* [ *True* | *False* ] whether the axes is | ||
visible | ||
@@ -476,6 +483,14 @@ def __init__(self, fig, rect, | ||
*yticklabels* sequence of strings | ||
*yticks* sequence of floats | ||
================ ========================================= | ||
.. warning:: | ||
Setting `share_tickers` to `False` and changing the | ||
`Locator`s of a shared axis may not play with autoscaling. | ||
Autoscaling may need to access the `Locator` object of the | ||
base axis. Normally, with `share_tickers=True`, the axes | ||
are guaranteed to share a `Locator` instance. | ||
""" % {'scale': ' | '.join( | ||
[repr(x) for x in mscale.get_scale_names()])} | ||
martist.Artist.__init__(self) | ||
@@ -493,6 +508,10 @@ def __init__(self, fig, rect, | ||
self.set_anchor('C') | ||
self._sharex = sharex | ||
self._sharey = sharey | ||
# share_tickers is only used as a modifier for sharex/y. It | ||
# should not remain in kwargs by the time kwargs updates the | ||
# instance dictionary. | ||
self._share_tickers = kwargs.pop('share_tickers', True) | ||
if sharex is not None: | ||
self._shared_x_axes.join(self, sharex) | ||
if sharex._adjustable == 'box': | ||
@@ -968,50 +987,52 @@ def cla(self): | ||
self.callbacks = cbook.CallbackRegistry() | ||
if self._sharex is not None: | ||
# The tickers need to exist but can be empty until after the | ||
# call to Axis._set_scale since they will be overwritten | ||
# anyway | ||
self.xaxis.major = maxis.Ticker() | ||
self.xaxis.minor = maxis.Ticker() | ||
# Copy the axis limits | ||
x0, x1 = self._sharex.get_xlim() | ||
self.set_xlim(x0, x1, emit=False, auto=None) | ||
# This overwrites the current formatter/locator | ||
self.xaxis._set_scale(self._sharex.xaxis.get_scale()) | ||
# Reset the formatter/locator. Axis handle gets marked as | ||
# stale in previous line, no need to repeat. | ||
if self._share_tickers: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I am confused, it looks like this behavior is what Ithought it was, but the other behavior is what it used to do. ContributorAuthor
| ||
self.xaxis.major = self._sharex.xaxis.major | ||
self.xaxis.minor = self._sharex.xaxis.minor | ||
else: | ||
self.xaxis.major.update_from(self._sharex.xaxis.major) | ||
self.xaxis.minor.update_from(self._sharex.xaxis.minor) | ||
else: | ||
self.xaxis._set_scale('linear') | ||
if self._sharey is not None: | ||
# The tickers need to exist but can be empty until after the | ||
# call to Axis._set_scale since they will be overwritten | ||
# anyway | ||
self.yaxis.major = maxis.Ticker() | ||
self.yaxis.minor = maxis.Ticker() | ||
# Copy the axis limits | ||
y0, y1 = self._sharey.get_ylim() | ||
self.set_ylim(y0, y1, emit=False, auto=None) | ||
# This overwrites the current formatter/locator | ||
self.yaxis._set_scale(self._sharey.yaxis.get_scale()) | ||
# Reset the formatter/locator. Axis handle gets marked as | ||
# stale in previous line, no need to repeat. | ||
if self._share_tickers: | ||
self.yaxis.major = self._sharey.yaxis.major | ||
self.yaxis.minor = self._sharey.yaxis.minor | ||
else: | ||
self.yaxis.major.update_from(self._sharey.yaxis.major) | ||
self.yaxis.minor.update_from(self._sharey.yaxis.minor) | ||
else: | ||
self.yaxis._set_scale('linear') | ||
@@ -3898,15 +3919,29 @@ def _make_twin_axes(self, *kl, **kwargs): | ||
ax2 = self.figure.add_axes(self.get_position(True), *kl, **kwargs) | ||
return ax2 | ||
def twinx(self, share_tickers=True): | ||
""" | ||
Create a twin Axes sharing the xaxis. | ||
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 :class:`matplotlib.ticker.LinearLocator`. | ||
`share_tickers` determines if the shared axis will always have | ||
the same major and minor `Formatter` and `Locator` objects as | ||
this one. This is usually desirable since the axes overlap. | ||
However, if one of the axes is shifted so that they are both | ||
visible, it may be useful to set this parameter to ``False``. | ||
.. warning:: | ||
Setting `share_tickers` to `False` and modifying the | ||
`Locator` of either axis may cause problems with | ||
autoscaling. Autoscaling may require access to the | ||
`Locator`, so the behavior will be undefined if the base and | ||
twinned axis do not share a `Locator` instance. | ||
Returns | ||
------- | ||
@@ -3918,7 +3953,7 @@ def twinx(self): | ||
For those who are 'picking' artists while using twinx, pick | ||
events are only called for the artists in the top-most axes. | ||
""" | ||
ax2 = self._make_twin_axes(sharex=self, share_tickers=share_tickers) | ||
ax2.yaxis.tick_right() | ||
ax2.yaxis.set_label_position('right') | ||
ax2.yaxis.set_offset_position('right') | ||
@@ -3928,15 +3963,29 @@ def twinx(self): | ||
ax2.patch.set_visible(False) | ||
return ax2 | ||
def twiny(self, share_tickers=True): | ||
""" | ||
Create a twin Axes sharing the yaxis. | ||
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 :class:`matplotlib.ticker.LinearLocator` | ||
`share_tickers` determines if the shared axis will always have | ||
the same major and minor `Formatter` and `Locator` objects as | ||
this one. This is usually desirable since the axes overlap. | ||
However, if one of the axes is shifted so that they are both | ||
visible, it may be useful to set this parameter to ``False``. | ||
.. warning:: | ||
Setting `share_tickers` to `False`andmodifying the | ||
`Locator` of either axis may cause problems with | ||
autoscaling. Autoscaling may require access to the | ||
`Locator`, sothebehavior will be undefined if the base and | ||
twinned axis do not share a `Locator` instance. | ||
Returns | ||
------- | ||
@@ -3948,7 +3997,7 @@ def twiny(self): | ||
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, share_tickers=share_tickers) | ||
ax2.xaxis.tick_top() | ||
ax2.xaxis.set_label_position('top') | ||
ax2.set_autoscaley_on(self.get_autoscaley_on()) | ||