Skip to content

Commit 1c3c32f

Browse files
authored
Merge pull request matplotlib#30367 from anntzer/st
Support passing xticks/yticks when constructing secondary_axis.
2 parents e99f7f8 + a0bc7e9 commit 1c3c32f

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

lib/matplotlib/axes/_secondary_axes.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import numbers
23

34
import numpy as np
@@ -145,10 +146,25 @@ def apply_aspect(self, position=None):
145146
self._set_lims()
146147
super().apply_aspect(position)
147148

148-
@_docstring.copy(Axis.set_ticks)
149-
def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs):
150-
ret = self._axis.set_ticks(ticks, labels, minor=minor, **kwargs)
151-
self.stale = True
149+
@functools.wraps(_AxesBase.set_xticks)
150+
def set_xticks(self, *args, **kwargs):
151+
if self._orientation == "y":
152+
raise TypeError("Cannot set xticks on a secondary y-axis")
153+
ret = super().set_xticks(*args, **kwargs)
154+
self._ticks_set = True
155+
return ret
156+
157+
@functools.wraps(_AxesBase.set_yticks)
158+
def set_yticks(self, *args, **kwargs):
159+
if self._orientation == "x":
160+
raise TypeError("Cannot set yticks on a secondary x-axis")
161+
ret = super().set_yticks(*args, **kwargs)
162+
self._ticks_set = True
163+
return ret
164+
165+
@functools.wraps(Axis.set_ticks)
166+
def set_ticks(self, *args, **kwargs):
167+
ret = self._axis.set_ticks(*args, **kwargs)
152168
self._ticks_set = True
153169
return ret
154170

lib/matplotlib/axes/_secondary_axes.pyi

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ class SecondaryAxis(_AxesBase):
2929
location: Literal["top", "bottom", "right", "left"] | float,
3030
transform: Transform | None = ...
3131
) -> None: ...
32+
def set_xticks(
33+
self,
34+
ticks: ArrayLike,
35+
labels: Iterable[str] | None = ...,
36+
*,
37+
minor: bool = ...,
38+
**kwargs
39+
) -> list[Tick]: ...
40+
def set_yticks(
41+
self,
42+
ticks: ArrayLike,
43+
labels: Iterable[str] | None = ...,
44+
*,
45+
minor: bool = ...,
46+
**kwargs
47+
) -> list[Tick]: ...
3248
def set_ticks(
3349
self,
3450
ticks: ArrayLike,

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8193,6 +8193,18 @@ def test_secondary_formatter():
81938193
secax.xaxis.get_major_formatter(), mticker.ScalarFormatter)
81948194

81958195

8196+
def test_secondary_init_xticks():
8197+
fig, ax = plt.subplots()
8198+
secax = ax.secondary_xaxis(1, xticks=[0, 1])
8199+
assert isinstance(secax.xaxis.get_major_locator(), mticker.FixedLocator)
8200+
with pytest.raises(TypeError):
8201+
secax.set_yticks([0, 1])
8202+
secax = ax.secondary_yaxis(1, yticks=[0, 1])
8203+
assert isinstance(secax.yaxis.get_major_locator(), mticker.FixedLocator)
8204+
with pytest.raises(TypeError):
8205+
secax.set_xticks([0, 1])
8206+
8207+
81968208
def test_secondary_repr():
81978209
fig, ax = plt.subplots()
81988210
secax = ax.secondary_xaxis("top")

0 commit comments

Comments
 (0)