Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 24bb51c

Browse files
authored
Fix tick_params() label rotation mode (#29593)
* Fix tick_params() label rotation mode Follow-up to #28968. The rotation_mode was only wired up half way, so that the parameter was accepted but did not have any effect. * Update axis.pyi
1 parent 5defe48 commit 24bb51c

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3469,7 +3469,9 @@ def tick_params(self, axis='both', **kwargs):
34693469
labelbottom, labeltop, labelleft, labelright : bool
34703470
Whether to draw the respective tick labels.
34713471
labelrotation : float
3472-
Tick label rotation
3472+
Tick label rotation angle in degrees. See `.Text.set_rotation`.
3473+
labelrotation_mode : {'default', 'anchor', 'xtick', 'ytick'}
3474+
Tick label rotation mode. See `.Text.set_rotation_mode`.
34733475
grid_color : :mpltype:`color`
34743476
Gridline color.
34753477
grid_alpha : float

lib/matplotlib/axis.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def __init__(
7575
label2On=False,
7676
major=True,
7777
labelrotation=0,
78+
labelrotation_mode=None,
7879
grid_color=None,
7980
grid_linestyle=None,
8081
grid_linewidth=None,
@@ -157,11 +158,13 @@ def __init__(
157158
self.label1 = mtext.Text(
158159
np.nan, np.nan,
159160
fontsize=labelsize, color=labelcolor, visible=label1On,
160-
fontfamily=labelfontfamily, rotation=self._labelrotation[1])
161+
fontfamily=labelfontfamily, rotation=self._labelrotation[1],
162+
rotation_mode=labelrotation_mode)
161163
self.label2 = mtext.Text(
162164
np.nan, np.nan,
163165
fontsize=labelsize, color=labelcolor, visible=label2On,
164-
fontfamily=labelfontfamily, rotation=self._labelrotation[1])
166+
fontfamily=labelfontfamily, rotation=self._labelrotation[1],
167+
rotation_mode=labelrotation_mode)
165168

166169
self._apply_tickdir(tickdir)
167170

@@ -321,7 +324,8 @@ def _apply_params(self, **kwargs):
321324
self.label2.set(rotation=self._labelrotation[1])
322325

323326
label_kw = {k[5:]: v for k, v in kwargs.items()
324-
if k in ['labelsize', 'labelcolor', 'labelfontfamily']}
327+
if k in ['labelsize', 'labelcolor', 'labelfontfamily',
328+
'labelrotation_mode']}
325329
self.label1.set(**label_kw)
326330
self.label2.set(**label_kw)
327331

@@ -1050,14 +1054,15 @@ def _translate_tick_params(cls, kw, reverse=False):
10501054
'tick1On', 'tick2On', 'label1On', 'label2On',
10511055
'length', 'direction', 'left', 'bottom', 'right', 'top',
10521056
'labelleft', 'labelbottom', 'labelright', 'labeltop',
1053-
'labelrotation', 'rotation_mode',
1057+
'labelrotation', 'labelrotation_mode',
10541058
*_gridline_param_names]
10551059

10561060
keymap = {
10571061
# tick_params key -> axis key
10581062
'length': 'size',
10591063
'direction': 'tickdir',
10601064
'rotation': 'labelrotation',
1065+
'rotation_mode': 'labelrotation_mode',
10611066
'left': 'tick1On',
10621067
'bottom': 'tick1On',
10631068
'right': 'tick2On',

lib/matplotlib/axis.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Tick(martist.Artist):
4848
label2On: bool = ...,
4949
major: bool = ...,
5050
labelrotation: float = ...,
51+
labelrotation_mode: Literal["default", "anchor", "xtick", "ytick"] | None = ...,
5152
grid_color: ColorType | None = ...,
5253
grid_linestyle: str | None = ...,
5354
grid_linewidth: float | None = ...,

lib/matplotlib/tests/test_axes.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7539,15 +7539,19 @@ def test_tick_param_label_rotation():
75397539
ax.yaxis.set_tick_params(which='both', rotation=90)
75407540
for text in ax.get_xticklabels(which='both'):
75417541
assert text.get_rotation() == 75
7542+
assert text.get_rotation_mode() == 'default'
75427543
for text in ax.get_yticklabels(which='both'):
75437544
assert text.get_rotation() == 90
7545+
assert text.get_rotation_mode() == 'default'
75447546

7545-
ax2.tick_params(axis='x', labelrotation=53)
7546-
ax2.tick_params(axis='y', rotation=35)
7547+
ax2.tick_params(axis='x', labelrotation=53, labelrotation_mode='xtick')
7548+
ax2.tick_params(axis='y', rotation=35, rotation_mode='ytick')
75477549
for text in ax2.get_xticklabels(which='major'):
75487550
assert text.get_rotation() == 53
7551+
assert text.get_rotation_mode() == 'xtick'
75497552
for text in ax2.get_yticklabels(which='major'):
75507553
assert text.get_rotation() == 35
7554+
assert text.get_rotation_mode() == 'ytick'
75517555

75527556

75537557
@mpl.style.context('default')

lib/matplotlib/text.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class Text(Artist):
4646
def update(self, kwargs: dict[str, Any]) -> list[Any]: ...
4747
def get_rotation(self) -> float: ...
4848
def get_transform_rotates_text(self) -> bool: ...
49-
def set_rotation_mode(self, m: None | Literal["default", "anchor"]) -> None: ...
50-
def get_rotation_mode(self) -> Literal["default", "anchor"]: ...
49+
def set_rotation_mode(self, m: None | Literal["default", "anchor", "xtick", "ytick"]) -> None: ...
50+
def get_rotation_mode(self) -> Literal["default", "anchor", "xtick", "ytick"]: ...
5151
def set_bbox(self, rectprops: dict[str, Any]) -> None: ...
5252
def get_bbox_patch(self) -> None | FancyBboxPatch: ...
5353
def update_bbox_position_size(self, renderer: RendererBase) -> None: ...

0 commit comments

Comments
 (0)