Skip to content

Commit fd4ea1d

Browse files
committed
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.
1 parent b0f015c commit fd4ea1d

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

lib/matplotlib/figure.py

+10-17
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,6 @@ def _suplabels(self, t, info, **kwargs):
287287
Additional kwargs are `matplotlib.text.Text` properties.
288288
"""
289289

290-
suplab = getattr(self, info['name'])
291-
292290
x = kwargs.pop('x', None)
293291
y = kwargs.pop('y', None)
294292
if info['name'] in ['_supxlabel', '_suptitle']:
@@ -300,29 +298,24 @@ def _suplabels(self, t, info, **kwargs):
300298
if y is None:
301299
y = info['y0']
302300

303-
if 'horizontalalignment' not in kwargs and 'ha' not in kwargs:
304-
kwargs['horizontalalignment'] = info['ha']
305-
if 'verticalalignment' not in kwargs and 'va' not in kwargs:
306-
kwargs['verticalalignment'] = info['va']
307-
if 'rotation' not in kwargs:
308-
kwargs['rotation'] = info['rotation']
301+
kwargs = cbook.normalize_kwargs(kwargs, Text)
302+
kwargs.setdefault('horizontalalignment', info['ha'])
303+
kwargs.setdefault('verticalalignment', info['va'])
304+
kwargs.setdefault('rotation', info['rotation'])
309305

310306
if 'fontproperties' not in kwargs:
311-
if 'fontsize' not in kwargs and 'size' not in kwargs:
312-
kwargs['size'] = mpl.rcParams[info['size']]
313-
if 'fontweight' not in kwargs and 'weight' not in kwargs:
314-
kwargs['weight'] = mpl.rcParams[info['weight']]
307+
kwargs.setdefault('fontsize', mpl.rcParams[info['size']])
308+
kwargs.setdefault('fontweight', mpl.rcParams[info['weight']])
315309

316-
sup = self.text(x, y, t, **kwargs)
310+
suplab = getattr(self, info['name'])
317311
if suplab is not None:
318312
suplab.set_text(t)
319313
suplab.set_position((x, y))
320-
suplab.update_from(sup)
321-
sup.remove()
314+
suplab.set(**kwargs)
322315
else:
323-
suplab = sup
316+
suplab = self.text(x, y, t, **kwargs)
317+
setattr(self, info['name'], suplab)
324318
suplab._autopos = autopos
325-
setattr(self, info['name'], suplab)
326319
self.stale = True
327320
return suplab
328321

lib/matplotlib/text.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,17 @@ def _get_text_metrics_with_cache_impl(
8080
@_docstring.interpd
8181
@_api.define_aliases({
8282
"color": ["c"],
83-
"fontfamily": ["family"],
8483
"fontproperties": ["font", "font_properties"],
85-
"horizontalalignment": ["ha"],
86-
"multialignment": ["ma"],
84+
"fontfamily": ["family"],
8785
"fontname": ["name"],
8886
"fontsize": ["size"],
8987
"fontstretch": ["stretch"],
9088
"fontstyle": ["style"],
9189
"fontvariant": ["variant"],
92-
"verticalalignment": ["va"],
9390
"fontweight": ["weight"],
91+
"horizontalalignment": ["ha"],
92+
"verticalalignment": ["va"],
93+
"multialignment": ["ma"],
9494
})
9595
class Text(Artist):
9696
"""Handle storing and drawing of text in window or data coordinates."""

0 commit comments

Comments
 (0)