Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1ab3a9f

Browse files
committed
STYLE: appease isort in gallery example
1 parent 39722fb commit 1ab3a9f

5 files changed

Lines changed: 48 additions & 30 deletions

File tree

doc/release/next_whats_new/twin_axes_zorder.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
Twin Axes ``zorder``
2-
--------------------
1+
Twin Axes ``delta_zorder``
2+
--------------------------
33

44
`~matplotlib.axes.Axes.twinx` and `~matplotlib.axes.Axes.twiny` now accept a
5-
*zorder* keyword argument to control whether the twin Axes is drawn in front of,
6-
or behind, the original Axes.
5+
*delta_zorder* keyword argument, a relative offset added to the original Axes'
6+
zorder, to control whether the twin Axes is drawn in front of, or behind, the
7+
original Axes. For example, pass ``delta_zorder=-1`` to easily draw a twin Axes
8+
behind the main Axes.
79

810
In addition, Matplotlib now automatically manages background patch visibility
911
for each group of twinned Axes so that only the bottom-most Axes in the group
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""
2-
=====================
3-
Twin Axes with zorder
4-
=====================
2+
===========================
3+
Twin Axes with delta_zorder
4+
===========================
55
66
`~matplotlib.axes.Axes.twinx` and `~matplotlib.axes.Axes.twiny` accept a
7-
*zorder* keyword argument that controls whether the twin Axes is drawn in front
8-
of or behind the original Axes.
7+
*delta_zorder* keyword argument (a relative offset added to the original Axes'
8+
zorder) that controls whether the twin Axes is drawn in front of or behind the
9+
original Axes.
910
1011
Matplotlib also automatically manages background patch visibility for twinned
1112
Axes groups so that only the bottom-most Axes has a visible background patch
@@ -16,15 +17,14 @@
1617
import matplotlib.pyplot as plt
1718
import numpy as np
1819

19-
2020
x = np.linspace(0, 10, 400)
2121
y_main = np.sin(x)
2222
y_twin = 0.4 * np.cos(x) + 0.6
2323

2424
fig, ax = plt.subplots()
2525

26-
# Put the twin Axes behind the original Axes.
27-
ax2 = ax.twinx(zorder=ax.get_zorder() - 1)
26+
# Put the twin Axes behind the original Axes (relative to the original zorder).
27+
ax2 = ax.twinx(delta_zorder=-1)
2828

2929
# Draw something broad on the twin Axes so that the stacking is obvious.
3030
ax2.fill_between(x, 0, y_twin, color="C1", alpha=0.35, label="twin fill")
@@ -38,7 +38,7 @@
3838
ax.set_xlabel("x")
3939
ax.set_ylabel("main y")
4040
ax2.set_ylabel("twin y")
41-
ax.set_title("Twin Axes drawn behind the main Axes using zorder")
41+
ax.set_title("Twin Axes drawn behind the main Axes using delta_zorder")
4242

4343
fig.tight_layout()
4444
plt.show()

lib/matplotlib/axes/_base.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4666,7 +4666,7 @@ def _update_twinned_axes_patch_visibility(self):
46664666
if patch is not None:
46674667
patch.set_visible((ax is bottom) and ax.get_frame_on())
46684668

4669-
def _make_twin_axes(self, *args, zorder=None, **kwargs):
4669+
def _make_twin_axes(self, *args, delta_zorder=None, **kwargs):
46704670
"""Make a twinx Axes of self. This is used for twinx and twiny."""
46714671
if 'sharex' in kwargs and 'sharey' in kwargs:
46724672
# The following line is added in v2.2 to avoid breaking Seaborn,
@@ -4683,13 +4683,15 @@ def _make_twin_axes(self, *args, zorder=None, **kwargs):
46834683
[0, 0, 1, 1], self.transAxes))
46844684
self.set_adjustable('datalim')
46854685
twin.set_adjustable('datalim')
4686-
twin.set_zorder(self.zorder if zorder is None else zorder)
4686+
original_zorder = self.get_zorder()
4687+
twin.set_zorder(original_zorder if delta_zorder is None
4688+
else original_zorder + delta_zorder)
46874689

46884690
self._twinned_axes.join(self, twin)
46894691
self._update_twinned_axes_patch_visibility()
46904692
return twin
46914693

4692-
def twinx(self, axes_class=None, *, zorder=None, **kwargs):
4694+
def twinx(self, axes_class=None, *, delta_zorder=None, **kwargs):
46934695
"""
46944696
Create a twin Axes sharing the xaxis.
46954697
@@ -4710,9 +4712,11 @@ def twinx(self, axes_class=None, *, zorder=None, **kwargs):
47104712
47114713
.. versionadded:: 3.11
47124714
4713-
zorder : float, optional
4714-
The zorder of the twin Axes. By default, the twin has the same
4715-
zorder as the original Axes.
4715+
delta_zorder : float, optional
4716+
A zorder offset for the twin Axes, relative to the original Axes.
4717+
The twin's zorder is set to ``self.get_zorder() + delta_zorder``.
4718+
By default (*delta_zorder* is None), the twin has the same zorder
4719+
as the original Axes.
47164720
47174721
kwargs : dict
47184722
The keyword arguments passed to `.Figure.add_subplot` or `.Figure.add_axes`.
@@ -4731,7 +4735,8 @@ def twinx(self, axes_class=None, *, zorder=None, **kwargs):
47314735
"""
47324736
if axes_class:
47334737
kwargs["axes_class"] = axes_class
4734-
ax2 = self._make_twin_axes(sharex=self, zorder=zorder, **kwargs)
4738+
ax2 = self._make_twin_axes(sharex=self, delta_zorder=delta_zorder,
4739+
**kwargs)
47354740
ax2.yaxis.tick_right()
47364741
ax2.yaxis.set_label_position('right')
47374742
ax2.yaxis.set_offset_position('right')
@@ -4741,7 +4746,7 @@ def twinx(self, axes_class=None, *, zorder=None, **kwargs):
47414746
ax2.xaxis.units = self.xaxis.units
47424747
return ax2
47434748

4744-
def twiny(self, axes_class=None, *, zorder=None, **kwargs):
4749+
def twiny(self, axes_class=None, *, delta_zorder=None, **kwargs):
47454750
"""
47464751
Create a twin Axes sharing the yaxis.
47474752
@@ -4762,9 +4767,11 @@ def twiny(self, axes_class=None, *, zorder=None, **kwargs):
47624767
47634768
.. versionadded:: 3.11
47644769
4765-
zorder : float, optional
4766-
The zorder of the twin Axes. By default, the twin has the same
4767-
zorder as the original Axes.
4770+
delta_zorder : float, optional
4771+
A zorder offset for the twin Axes, relative to the original Axes.
4772+
The twin's zorder is set to ``self.get_zorder() + delta_zorder``.
4773+
By default (*delta_zorder* is None), the twin has the same zorder
4774+
as the original Axes.
47684775
47694776
kwargs : dict
47704777
The keyword arguments passed to `.Figure.add_subplot` or `.Figure.add_axes`.
@@ -4783,7 +4790,8 @@ def twiny(self, axes_class=None, *, zorder=None, **kwargs):
47834790
"""
47844791
if axes_class:
47854792
kwargs["axes_class"] = axes_class
4786-
ax2 = self._make_twin_axes(sharey=self, zorder=zorder, **kwargs)
4793+
ax2 = self._make_twin_axes(sharey=self, delta_zorder=delta_zorder,
4794+
**kwargs)
47874795
ax2.xaxis.tick_top()
47884796
ax2.xaxis.set_label_position('top')
47894797
ax2.set_autoscaley_on(self.get_autoscaley_on())

lib/matplotlib/axes/_base.pyi

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,18 @@ class _AxesBase(martist.Artist):
387387
for_layout_only: bool = ...
388388
) -> Bbox | None: ...
389389
def twinx(
390-
self, axes_class: Axes | None = ..., *, zorder: float | None = ..., **kwargs
390+
self,
391+
axes_class: Axes | None = ...,
392+
*,
393+
delta_zorder: float | None = ...,
394+
**kwargs
391395
) -> Axes: ...
392396
def twiny(
393-
self, axes_class: Axes | None = ..., *, zorder: float | None = ..., **kwargs
397+
self,
398+
axes_class: Axes | None = ...,
399+
*,
400+
delta_zorder: float | None = ...,
401+
**kwargs
394402
) -> Axes: ...
395403
def get_shared_x_axes(self) -> cbook.GrouperView: ...
396404
def get_shared_y_axes(self) -> cbook.GrouperView: ...

lib/matplotlib/tests/test_axes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8075,7 +8075,7 @@ def test_twinning_patch_visibility_default():
80758075

80768076
def test_twinning_patch_visibility_respects_zorder():
80778077
_, ax = plt.subplots()
8078-
ax2 = ax.twinx(zorder=ax.get_zorder() - 1)
8078+
ax2 = ax.twinx(delta_zorder=-1)
80798079
assert ax2.get_zorder() == ax.get_zorder() - 1
80808080
assert ax2.patch.get_visible()
80818081
assert not ax.patch.get_visible()
@@ -8092,8 +8092,8 @@ def test_twinning_patch_visibility_multiple_twins_same_zorder():
80928092

80938093
def test_twinning_patch_visibility_updates_for_new_bottom():
80948094
_, ax = plt.subplots()
8095-
ax2 = ax.twinx(zorder=ax.get_zorder() - 1)
8096-
ax3 = ax.twinx(zorder=ax.get_zorder() - 2)
8095+
ax2 = ax.twinx(delta_zorder=-1)
8096+
ax3 = ax.twinx(delta_zorder=-2)
80978097
assert ax3.patch.get_visible()
80988098
assert not ax2.patch.get_visible()
80998099
assert not ax.patch.get_visible()

0 commit comments

Comments
 (0)