From 82229fb6bd3c8e7e5e5fe762a3131bfdaf3305ec Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sat, 8 Feb 2025 00:02:38 +0100 Subject: [PATCH 1/2] 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. --- lib/matplotlib/axes/_base.py | 4 +++- lib/matplotlib/axis.py | 13 +++++++++---- lib/matplotlib/axis.pyi | 1 + lib/matplotlib/tests/test_axes.py | 8 ++++++-- lib/matplotlib/text.pyi | 4 ++-- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 18c57a37db89..e90bf7640c67 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -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 diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index c33523f628c9..19096fc29d3e 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -75,6 +75,7 @@ def __init__( label2On=False, major=True, labelrotation=0, + labelrotation_mode=None, grid_color=None, grid_linestyle=None, grid_linewidth=None, @@ -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) @@ -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) @@ -1050,7 +1054,7 @@ 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 = { @@ -1058,6 +1062,7 @@ def _translate_tick_params(cls, kw, reverse=False): 'length': 'size', 'direction': 'tickdir', 'rotation': 'labelrotation', + 'rotation_mode': 'labelrotation_mode', 'left': 'tick1On', 'bottom': 'tick1On', 'right': 'tick2On', diff --git a/lib/matplotlib/axis.pyi b/lib/matplotlib/axis.pyi index f2c5b1fc586d..a15d2f6b5f4c 100644 --- a/lib/matplotlib/axis.pyi +++ b/lib/matplotlib/axis.pyi @@ -48,6 +48,7 @@ class Tick(martist.Artist): label2On: bool = ..., major: bool = ..., labelrotation: float = ..., + labelrotation_mode: Literal["default", "anchor", "xtick", "ytick"] = ..., grid_color: ColorType | None = ..., grid_linestyle: str | None = ..., grid_linewidth: float | None = ..., diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 10b6af8d5883..0025781d4901 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -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') diff --git a/lib/matplotlib/text.pyi b/lib/matplotlib/text.pyi index 74b5d9904a1d..36a805df4e04 100644 --- a/lib/matplotlib/text.pyi +++ b/lib/matplotlib/text.pyi @@ -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: ... From 67e0226e25d77434f455c7637899e9b987157e73 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sat, 8 Feb 2025 02:38:33 +0100 Subject: [PATCH 2/2] Update axis.pyi --- lib/matplotlib/axis.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/axis.pyi b/lib/matplotlib/axis.pyi index a15d2f6b5f4c..6119b946fd7b 100644 --- a/lib/matplotlib/axis.pyi +++ b/lib/matplotlib/axis.pyi @@ -48,7 +48,7 @@ class Tick(martist.Artist): label2On: bool = ..., major: bool = ..., labelrotation: float = ..., - labelrotation_mode: Literal["default", "anchor", "xtick", "ytick"] = ..., + labelrotation_mode: Literal["default", "anchor", "xtick", "ytick"] | None = ..., grid_color: ColorType | None = ..., grid_linestyle: str | None = ..., grid_linewidth: float | None = ...,