Skip to content

Commit b337f94

Browse files
committed
fix(comment): #29325 (comment)
1 parent 0476435 commit b337f94

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4596,7 +4596,7 @@ def _make_twin_axes(self, *args, **kwargs):
45964596
self._twinned_axes.join(self, twin)
45974597
return twin
45984598

4599-
def twinx(self, **kwargs):
4599+
def twinx(self, axes_class=None, **kwargs):
46004600
"""
46014601
Create a twin Axes sharing the xaxis.
46024602
@@ -4608,6 +4608,11 @@ def twinx(self, **kwargs):
46084608
46094609
Parameters
46104610
----------
4611+
axes_class : subclass type of `~.axes.Axes`, optional
4612+
The `.axes.Axes` subclass that is instantiated. This parameter
4613+
is incompatible with *projection* and *polar*. See
4614+
:ref:`axisartist_users-guide-index` for examples.
4615+
46114616
kwargs : dict
46124617
The keyword arguments passed to ``add_subplot()`` or ``add_axes()``.
46134618
@@ -4621,9 +4626,9 @@ def twinx(self, **kwargs):
46214626
For those who are 'picking' artists while using twinx, pick
46224627
events are only called for the artists in the top-most Axes.
46234628
"""
4624-
if not {"projection", "polar", "axes_class"}.intersection(kwargs):
4625-
kwargs["axes_class"] = type(self)
4626-
ax2 = self._make_twin_axes(sharex=self, axes_class=type(self), **kwargs)
4629+
if axes_class:
4630+
kwargs["axes_class"] = axes_class
4631+
ax2 = self._make_twin_axes(sharex=self, **kwargs)
46274632
ax2.yaxis.tick_right()
46284633
ax2.yaxis.set_label_position('right')
46294634
ax2.yaxis.set_offset_position('right')
@@ -4634,7 +4639,7 @@ def twinx(self, **kwargs):
46344639
ax2.xaxis.units = self.xaxis.units
46354640
return ax2
46364641

4637-
def twiny(self, **kwargs):
4642+
def twiny(self, axes_class=None, **kwargs):
46384643
"""
46394644
Create a twin Axes sharing the yaxis.
46404645
@@ -4646,6 +4651,11 @@ def twiny(self, **kwargs):
46464651
46474652
Parameters
46484653
----------
4654+
axes_class : subclass type of `~.axes.Axes`, optional
4655+
The `.axes.Axes` subclass that is instantiated. This parameter
4656+
is incompatible with *projection* and *polar*. See
4657+
:ref:`axisartist_users-guide-index` for examples.
4658+
46494659
kwargs : dict
46504660
The keyword arguments passed to ``add_subplot()`` or ``add_axes()``.
46514661
@@ -4659,9 +4669,9 @@ def twiny(self, **kwargs):
46594669
For those who are 'picking' artists while using twiny, pick
46604670
events are only called for the artists in the top-most Axes.
46614671
"""
4662-
if not {"projection", "polar", "axes_class"}.intersection(kwargs):
4663-
kwargs["axes_class"] = type(self)
4664-
ax2 = self._make_twin_axes(sharey=self, axes_class=type(self), **kwargs)
4672+
if axes_class:
4673+
kwargs["axes_class"] = axes_class
4674+
ax2 = self._make_twin_axes(sharey=self, **kwargs)
46654675
ax2.xaxis.tick_top()
46664676
ax2.xaxis.set_label_position('top')
46674677
ax2.set_autoscaley_on(self.get_autoscaley_on())

lib/matplotlib/axes/_base.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import datetime
44
from collections.abc import Callable, Iterable, Iterator, Sequence
55
from matplotlib import cbook
66
from matplotlib.artist import Artist
7+
from matplotlib.axes import Axes
78
from matplotlib.axis import XAxis, YAxis, Tick
89
from matplotlib.backend_bases import RendererBase, MouseButton, MouseEvent
910
from matplotlib.cbook import CallbackRegistry
@@ -28,7 +29,6 @@ import numpy as np
2829
from numpy.typing import ArrayLike
2930
from typing import Any, Literal, TypeVar, overload
3031
from matplotlib.typing import ColorType
31-
from typing_extensions import Self
3232

3333
_T = TypeVar("_T", bound=Artist)
3434

@@ -385,8 +385,8 @@ class _AxesBase(martist.Artist):
385385
bbox_extra_artists: Sequence[Artist] | None = ...,
386386
for_layout_only: bool = ...
387387
) -> Bbox | None: ...
388-
def twinx(self, **kwargs) -> Self: ...
389-
def twiny(self, **kwargs) -> Self: ...
388+
def twinx(self, axes_class: Axes | None = ..., **kwargs) -> Axes: ...
389+
def twiny(self, axes_class: Axes | None = ..., **kwargs) -> Axes: ...
390390
def get_shared_x_axes(self) -> cbook.GrouperView: ...
391391
def get_shared_y_axes(self) -> cbook.GrouperView: ...
392392
def label_outer(self, remove_inner_ticks: bool = ...) -> None: ...

lib/matplotlib/tests/test_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7548,7 +7548,7 @@ def test_twinx_subclass(axes_class, kw0, kw1):
75487548
for k, v in kw0.items():
75497549
assert getattr(classed_ax, k) == v
75507550

7551-
twin = classed_ax.twinx(**kw1)
7551+
twin = classed_ax.twinx(axes_class=axes_class, **kw1)
75527552
assert type(twin) is axes_class
75537553
for k, v in kw1.items():
75547554
assert getattr(twin, k) == v

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy