Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8d3c4db

Browse filesBrowse files
cmp0xfftimhoffmrcomer
authored
Propagate Axes class and kwargs for twinx and twiny (#29325)
* feat: axes class and kwargs for twinx and twiny * fix: projection or polar * feat: axes class and kwargs for twinx and twiny * fix(comment): #29325 (comment) * fix(codecov): coverage * Apply suggestions from code review - https://github.com/matplotlib/matplotlib/pull/29325/files#r1894693106 - https://github.com/matplotlib/matplotlib/pull/29325/files#r1894693188 - https://github.com/matplotlib/matplotlib/pull/29325/files#r1894696478 Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> * Apply suggestions from code review - #29325 (comment) - #29325 (comment) - #29325 (comment) Co-authored-by: Ruth Comer <10599679+rcomer@users.noreply.github.com> --------- Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Co-authored-by: Ruth Comer <10599679+rcomer@users.noreply.github.com>
1 parent a61e118 commit 8d3c4db
Copy full SHA for 8d3c4db

File tree

Expand file treeCollapse file tree

3 files changed

+71
-6
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+71
-6
lines changed

‎lib/matplotlib/axes/_base.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.py
+40-4Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4592,7 +4592,7 @@ def _make_twin_axes(self, *args, **kwargs):
45924592
self._twinned_axes.join(self, twin)
45934593
return twin
45944594

4595-
def twinx(self):
4595+
def twinx(self, axes_class=None, **kwargs):
45964596
"""
45974597
Create a twin Axes sharing the xaxis.
45984598
@@ -4602,6 +4602,22 @@ def twinx(self):
46024602
Axes. To ensure that the tick marks of both y-axes align, see
46034603
`~matplotlib.ticker.LinearLocator`.
46044604
4605+
Parameters
4606+
----------
4607+
axes_class : subclass type of `~.axes.Axes`, optional
4608+
The `.axes.Axes` subclass that is instantiated. This parameter
4609+
is incompatible with *projection* and *polar*. See
4610+
:ref:`axisartist_users-guide-index` for examples.
4611+
4612+
By default, `~.axes.Axes` is used.
4613+
4614+
.. versionadded:: 3.11
4615+
4616+
kwargs : dict
4617+
The keyword arguments passed to `.Figure.add_subplot` or `.Figure.add_axes`.
4618+
4619+
.. versionadded:: 3.11
4620+
46054621
Returns
46064622
-------
46074623
Axes
@@ -4612,7 +4628,9 @@ def twinx(self):
46124628
For those who are 'picking' artists while using twinx, pick
46134629
events are only called for the artists in the top-most Axes.
46144630
"""
4615-
ax2 = self._make_twin_axes(sharex=self)
4631+
if axes_class:
4632+
kwargs["axes_class"] = axes_class
4633+
ax2 = self._make_twin_axes(sharex=self, **kwargs)
46164634
ax2.yaxis.tick_right()
46174635
ax2.yaxis.set_label_position('right')
46184636
ax2.yaxis.set_offset_position('right')
@@ -4623,7 +4641,7 @@ def twinx(self):
46234641
ax2.xaxis.units = self.xaxis.units
46244642
return ax2
46254643

4626-
def twiny(self):
4644+
def twiny(self, axes_class=None, **kwargs):
46274645
"""
46284646
Create a twin Axes sharing the yaxis.
46294647
@@ -4633,6 +4651,22 @@ def twiny(self):
46334651
To ensure that the tick marks of both x-axes align, see
46344652
`~matplotlib.ticker.LinearLocator`.
46354653
4654+
Parameters
4655+
----------
4656+
axes_class : subclass type of `~.axes.Axes`, optional
4657+
The `.axes.Axes` subclass that is instantiated. This parameter
4658+
is incompatible with *projection* and *polar*. See
4659+
:ref:`axisartist_users-guide-index` for examples.
4660+
4661+
By default, `~.axes.Axes` is used.
4662+
4663+
.. versionadded:: 3.11
4664+
4665+
kwargs : dict
4666+
The keyword arguments passed to `.Figure.add_subplot` or `.Figure.add_axes`.
4667+
4668+
.. versionadded:: 3.11
4669+
46364670
Returns
46374671
-------
46384672
Axes
@@ -4643,7 +4677,9 @@ def twiny(self):
46434677
For those who are 'picking' artists while using twiny, pick
46444678
events are only called for the artists in the top-most Axes.
46454679
"""
4646-
ax2 = self._make_twin_axes(sharey=self)
4680+
if axes_class:
4681+
kwargs["axes_class"] = axes_class
4682+
ax2 = self._make_twin_axes(sharey=self, **kwargs)
46474683
ax2.xaxis.tick_top()
46484684
ax2.xaxis.set_label_position('top')
46494685
ax2.set_autoscaley_on(self.get_autoscaley_on())

‎lib/matplotlib/axes/_base.pyi

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.pyi
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ class _AxesBase(martist.Artist):
386386
bbox_extra_artists: Sequence[Artist] | None = ...,
387387
for_layout_only: bool = ...
388388
) -> Bbox | None: ...
389-
def twinx(self) -> Axes: ...
390-
def twiny(self) -> Axes: ...
389+
def twinx(self, axes_class: Axes | None = ..., **kwargs) -> Axes: ...
390+
def twiny(self, axes_class: Axes | None = ..., **kwargs) -> Axes: ...
391391
def get_shared_x_axes(self) -> cbook.GrouperView: ...
392392
def get_shared_y_axes(self) -> cbook.GrouperView: ...
393393
def label_outer(self, remove_inner_ticks: bool = ...) -> None: ...

‎lib/matplotlib/tests/test_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7599,6 +7599,35 @@ def test_twinx_knows_limits():
75997599
assert_array_equal(xtwin.viewLim.intervalx, ax2.viewLim.intervalx)
76007600

76017601

7602+
class SubclassAxes(Axes):
7603+
def __init__(self, *args, foo, **kwargs):
7604+
super().__init__(*args, **kwargs)
7605+
self.foo = foo
7606+
7607+
7608+
def test_twinning_with_axes_class():
7609+
"""Check that twinx/y(axes_class=...) gives the appropriate class."""
7610+
_, ax = plt.subplots()
7611+
twinx = ax.twinx(axes_class=SubclassAxes, foo=1)
7612+
assert isinstance(twinx, SubclassAxes)
7613+
assert twinx.foo == 1
7614+
twiny = ax.twiny(axes_class=SubclassAxes, foo=2)
7615+
assert isinstance(twiny, SubclassAxes)
7616+
assert twiny.foo == 2
7617+
7618+
7619+
def test_twinning_default_axes_class():
7620+
"""
7621+
Check that the default class for twinx/y() is Axes,
7622+
even if the original is an Axes subclass.
7623+
"""
7624+
_, ax = plt.subplots(subplot_kw=dict(axes_class=SubclassAxes, foo=1))
7625+
twinx = ax.twinx()
7626+
assert type(twinx) is Axes
7627+
twiny = ax.twiny()
7628+
assert type(twiny) is Axes
7629+
7630+
76027631
def test_zero_linewidth():
76037632
# Check that setting a zero linewidth doesn't error
76047633
plt.plot([0, 1], [0, 1], ls='--', lw=0)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.