From fd4ea1d6bb617121e76f3c464dc938c91b268a17 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 1 Feb 2024 09:05:08 +0100 Subject: [PATCH] Simplify Figure._suplabels. Rely on alias normalization machinery to make alias checking simpler and more robust (there are also aliases for fontproperties which were not checked before). While at it, also slightly reorder Text aliases (this has no actual visible effect) to have the font properties and the alignments grouped. --- lib/matplotlib/figure.py | 27 ++++++++++----------------- lib/matplotlib/text.py | 8 ++++---- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 2cd1436ed1c9..388f2c4f64ea 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -287,8 +287,6 @@ def _suplabels(self, t, info, **kwargs): Additional kwargs are `matplotlib.text.Text` properties. """ - suplab = getattr(self, info['name']) - x = kwargs.pop('x', None) y = kwargs.pop('y', None) if info['name'] in ['_supxlabel', '_suptitle']: @@ -300,29 +298,24 @@ def _suplabels(self, t, info, **kwargs): if y is None: y = info['y0'] - if 'horizontalalignment' not in kwargs and 'ha' not in kwargs: - kwargs['horizontalalignment'] = info['ha'] - if 'verticalalignment' not in kwargs and 'va' not in kwargs: - kwargs['verticalalignment'] = info['va'] - if 'rotation' not in kwargs: - kwargs['rotation'] = info['rotation'] + kwargs = cbook.normalize_kwargs(kwargs, Text) + kwargs.setdefault('horizontalalignment', info['ha']) + kwargs.setdefault('verticalalignment', info['va']) + kwargs.setdefault('rotation', info['rotation']) if 'fontproperties' not in kwargs: - if 'fontsize' not in kwargs and 'size' not in kwargs: - kwargs['size'] = mpl.rcParams[info['size']] - if 'fontweight' not in kwargs and 'weight' not in kwargs: - kwargs['weight'] = mpl.rcParams[info['weight']] + kwargs.setdefault('fontsize', mpl.rcParams[info['size']]) + kwargs.setdefault('fontweight', mpl.rcParams[info['weight']]) - sup = self.text(x, y, t, **kwargs) + suplab = getattr(self, info['name']) if suplab is not None: suplab.set_text(t) suplab.set_position((x, y)) - suplab.update_from(sup) - sup.remove() + suplab.set(**kwargs) else: - suplab = sup + suplab = self.text(x, y, t, **kwargs) + setattr(self, info['name'], suplab) suplab._autopos = autopos - setattr(self, info['name'], suplab) self.stale = True return suplab diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index da11194c6427..ca9aca895f8f 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -80,17 +80,17 @@ def _get_text_metrics_with_cache_impl( @_docstring.interpd @_api.define_aliases({ "color": ["c"], - "fontfamily": ["family"], "fontproperties": ["font", "font_properties"], - "horizontalalignment": ["ha"], - "multialignment": ["ma"], + "fontfamily": ["family"], "fontname": ["name"], "fontsize": ["size"], "fontstretch": ["stretch"], "fontstyle": ["style"], "fontvariant": ["variant"], - "verticalalignment": ["va"], "fontweight": ["weight"], + "horizontalalignment": ["ha"], + "verticalalignment": ["va"], + "multialignment": ["ma"], }) class Text(Artist): """Handle storing and drawing of text in window or data coordinates."""