Skip to content

Support passing xticks/yticks when constructing secondary_axis. #30367

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions lib/matplotlib/axes/_secondary_axes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import numbers

import numpy as np
Expand Down Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions lib/matplotlib/axes/_secondary_axes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading
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