Skip to content

Fix tick_params() label rotation mode #29593

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 2 commits into from
Feb 11, 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
4 changes: 3 additions & 1 deletion lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3469,7 +3469,9 @@ def tick_params(self, axis='both', **kwargs):
labelbottom, labeltop, labelleft, labelright : bool
Whether to draw the respective tick labels.
labelrotation : float
Tick label rotation
Tick label rotation angle in degrees. See `.Text.set_rotation`.
labelrotation_mode : {'default', 'anchor', 'xtick', 'ytick'}
Tick label rotation mode. See `.Text.set_rotation_mode`.
grid_color : :mpltype:`color`
Gridline color.
grid_alpha : float
Expand Down
13 changes: 9 additions & 4 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def __init__(
label2On=False,
major=True,
labelrotation=0,
labelrotation_mode=None,
grid_color=None,
grid_linestyle=None,
grid_linewidth=None,
Expand Down Expand Up @@ -157,11 +158,13 @@ def __init__(
self.label1 = mtext.Text(
np.nan, np.nan,
fontsize=labelsize, color=labelcolor, visible=label1On,
fontfamily=labelfontfamily, rotation=self._labelrotation[1])
fontfamily=labelfontfamily, rotation=self._labelrotation[1],
rotation_mode=labelrotation_mode)
self.label2 = mtext.Text(
np.nan, np.nan,
fontsize=labelsize, color=labelcolor, visible=label2On,
fontfamily=labelfontfamily, rotation=self._labelrotation[1])
fontfamily=labelfontfamily, rotation=self._labelrotation[1],
rotation_mode=labelrotation_mode)

self._apply_tickdir(tickdir)

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

label_kw = {k[5:]: v for k, v in kwargs.items()
if k in ['labelsize', 'labelcolor', 'labelfontfamily']}
if k in ['labelsize', 'labelcolor', 'labelfontfamily',
'labelrotation_mode']}
self.label1.set(**label_kw)
self.label2.set(**label_kw)

Expand Down Expand Up @@ -1050,14 +1054,15 @@ def _translate_tick_params(cls, kw, reverse=False):
'tick1On', 'tick2On', 'label1On', 'label2On',
'length', 'direction', 'left', 'bottom', 'right', 'top',
'labelleft', 'labelbottom', 'labelright', 'labeltop',
'labelrotation', 'rotation_mode',
'labelrotation', 'labelrotation_mode',
*_gridline_param_names]

keymap = {
# tick_params key -> axis key
'length': 'size',
'direction': 'tickdir',
'rotation': 'labelrotation',
'rotation_mode': 'labelrotation_mode',
'left': 'tick1On',
'bottom': 'tick1On',
'right': 'tick2On',
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/axis.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Tick(martist.Artist):
label2On: bool = ...,
major: bool = ...,
labelrotation: float = ...,
labelrotation_mode: Literal["default", "anchor", "xtick", "ytick"] | None = ...,
grid_color: ColorType | None = ...,
grid_linestyle: str | None = ...,
grid_linewidth: float | None = ...,
Expand Down
8 changes: 6 additions & 2 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7539,15 +7539,19 @@ def test_tick_param_label_rotation():
ax.yaxis.set_tick_params(which='both', rotation=90)
for text in ax.get_xticklabels(which='both'):
assert text.get_rotation() == 75
assert text.get_rotation_mode() == 'default'
for text in ax.get_yticklabels(which='both'):
assert text.get_rotation() == 90
assert text.get_rotation_mode() == 'default'

ax2.tick_params(axis='x', labelrotation=53)
ax2.tick_params(axis='y', rotation=35)
ax2.tick_params(axis='x', labelrotation=53, labelrotation_mode='xtick')
ax2.tick_params(axis='y', rotation=35, rotation_mode='ytick')
for text in ax2.get_xticklabels(which='major'):
assert text.get_rotation() == 53
assert text.get_rotation_mode() == 'xtick'
for text in ax2.get_yticklabels(which='major'):
assert text.get_rotation() == 35
assert text.get_rotation_mode() == 'ytick'


@mpl.style.context('default')
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/text.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class Text(Artist):
def update(self, kwargs: dict[str, Any]) -> list[Any]: ...
def get_rotation(self) -> float: ...
def get_transform_rotates_text(self) -> bool: ...
def set_rotation_mode(self, m: None | Literal["default", "anchor"]) -> None: ...
def get_rotation_mode(self) -> Literal["default", "anchor"]: ...
def set_rotation_mode(self, m: None | Literal["default", "anchor", "xtick", "ytick"]) -> None: ...
def get_rotation_mode(self) -> Literal["default", "anchor", "xtick", "ytick"]: ...
def set_bbox(self, rectprops: dict[str, Any]) -> None: ...
def get_bbox_patch(self) -> None | FancyBboxPatch: ...
def update_bbox_position_size(self, renderer: RendererBase) -> None: ...
Expand Down