From a0bc7e981772d6475acd512334d2362fb9602819 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 29 Jul 2025 15:07:22 +0200 Subject: [PATCH] Support passing xticks/yticks when constructing secondary_axis. Previously, passing xticks to a secondary_xaxis call, or similarly yticks to secondary_yaxis (see added test), would get silently ignored, because the set ticks would get overwritten in _set_scale (called by set_functions). Fix that by toggling the secax._ticks_set private flag, which already exists for that purpose in set_ticks. While at it, also simplify the implementation of set_ticks by relying on functools.wraps to copy the relevant signature. --- lib/matplotlib/axes/_secondary_axes.py | 24 ++++++++++++++++++++---- lib/matplotlib/axes/_secondary_axes.pyi | 16 ++++++++++++++++ lib/matplotlib/tests/test_axes.py | 12 ++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_secondary_axes.py b/lib/matplotlib/axes/_secondary_axes.py index 15a1970fa4a6..08e706bba245 100644 --- a/lib/matplotlib/axes/_secondary_axes.py +++ b/lib/matplotlib/axes/_secondary_axes.py @@ -1,3 +1,4 @@ +import functools import numbers import numpy as np @@ -145,10 +146,25 @@ def apply_aspect(self, position=None): self._set_lims() super().apply_aspect(position) - @_docstring.copy(Axis.set_ticks) - def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs): - ret = self._axis.set_ticks(ticks, labels, minor=minor, **kwargs) - self.stale = True + @functools.wraps(_AxesBase.set_xticks) + def set_xticks(self, *args, **kwargs): + if self._orientation == "y": + raise TypeError("Cannot set xticks on a secondary y-axis") + ret = super().set_xticks(*args, **kwargs) + self._ticks_set = True + return ret + + @functools.wraps(_AxesBase.set_yticks) + def set_yticks(self, *args, **kwargs): + if self._orientation == "x": + raise TypeError("Cannot set yticks on a secondary x-axis") + ret = super().set_yticks(*args, **kwargs) + self._ticks_set = True + return ret + + @functools.wraps(Axis.set_ticks) + def set_ticks(self, *args, **kwargs): + ret = self._axis.set_ticks(*args, **kwargs) self._ticks_set = True return ret diff --git a/lib/matplotlib/axes/_secondary_axes.pyi b/lib/matplotlib/axes/_secondary_axes.pyi index afb429f740c4..92bba590a4fa 100644 --- a/lib/matplotlib/axes/_secondary_axes.pyi +++ b/lib/matplotlib/axes/_secondary_axes.pyi @@ -29,6 +29,22 @@ class SecondaryAxis(_AxesBase): location: Literal["top", "bottom", "right", "left"] | float, transform: Transform | None = ... ) -> None: ... + def set_xticks( + self, + ticks: ArrayLike, + labels: Iterable[str] | None = ..., + *, + minor: bool = ..., + **kwargs + ) -> list[Tick]: ... + def set_yticks( + self, + ticks: ArrayLike, + labels: Iterable[str] | None = ..., + *, + minor: bool = ..., + **kwargs + ) -> list[Tick]: ... def set_ticks( self, ticks: ArrayLike, diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index c96173e340f7..f03183b20323 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -8193,6 +8193,18 @@ def test_secondary_formatter(): secax.xaxis.get_major_formatter(), mticker.ScalarFormatter) +def test_secondary_init_xticks(): + fig, ax = plt.subplots() + secax = ax.secondary_xaxis(1, xticks=[0, 1]) + assert isinstance(secax.xaxis.get_major_locator(), mticker.FixedLocator) + with pytest.raises(TypeError): + secax.set_yticks([0, 1]) + secax = ax.secondary_yaxis(1, yticks=[0, 1]) + assert isinstance(secax.yaxis.get_major_locator(), mticker.FixedLocator) + with pytest.raises(TypeError): + secax.set_xticks([0, 1]) + + def test_secondary_repr(): fig, ax = plt.subplots() secax = ax.secondary_xaxis("top") 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