Skip to content

Commit d961462

Browse files
committed
text: Set line spacing to 'normal' by default
This follows from CSS' default for line height. At the moment, the behaviour has not been changed, and still just falls back to 1.2 for 'normal'.
1 parent 3ab6a27 commit d961462

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

lib/matplotlib/text.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def _reset_visual_defaults(
241241
self._bbox_patch = None # a FancyBboxPatch instance
242242
self._renderer = None
243243
if linespacing is None:
244-
linespacing = 1.2 # Maybe use rcParam later.
244+
linespacing = 'normal' # Maybe use rcParam later.
245245
self.set_linespacing(linespacing)
246246
self.set_rotation_mode(rotation_mode)
247247
self.set_antialiased(mpl._val_or_rc(antialiased, 'text.antialiased'))
@@ -439,7 +439,8 @@ def _get_layout(self, renderer):
439439
ismath="TeX" if self.get_usetex() else False,
440440
dpi=self.get_figure(root=True).dpi)
441441
lp_a = lp_h - lp_d
442-
min_dy = lp_a * self._linespacing
442+
linespacing = 1.2 if self._linespacing == 'normal' else self._linespacing
443+
min_dy = lp_a * linespacing
443444

444445
for i, line in enumerate(lines):
445446
clean_line, ismath = self._preprocess_math(line)
@@ -462,7 +463,7 @@ def _get_layout(self, renderer):
462463
if i == 0: # position at baseline
463464
thisy = -a
464465
else: # put baseline a good distance from bottom of previous line
465-
thisy -= max(min_dy, a * self._linespacing)
466+
thisy -= max(min_dy, a * linespacing)
466467

467468
wads.append((w, a, d))
468469
xs.append(thisx) # == 0.
@@ -1122,18 +1123,26 @@ def set_multialignment(self, align):
11221123

11231124
def set_linespacing(self, spacing):
11241125
"""
1125-
Set the line spacing as a multiple of the font size.
1126-
1127-
The default line spacing is 1.2.
1126+
Set the line spacing.
11281127
11291128
Parameters
11301129
----------
1131-
spacing : float (multiple of font size)
1130+
spacing : 'normal' or float, default: 'normal'
1131+
If 'normal', then the line spacing is automatically determined by font
1132+
metrics for each line individually.
1133+
1134+
If a float, then line spacing will be fixed to this multiple of the font
1135+
size for every line.
11321136
"""
1133-
_api.check_isinstance(Real, spacing=spacing)
1137+
if not cbook._str_equal(spacing, 'normal'):
1138+
_api.check_isinstance(Real, spacing=spacing)
11341139
self._linespacing = spacing
11351140
self.stale = True
11361141

1142+
def get_linespacing(self):
1143+
"""Get the line spacing."""
1144+
return self._linespacing
1145+
11371146
def set_fontfamily(self, fontname):
11381147
"""
11391148
Set the font family. Can be either a single string, or a list of

lib/matplotlib/text.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Text(Artist):
3434
multialignment: Literal["left", "center", "right"] | None = ...,
3535
fontproperties: str | Path | FontProperties | None = ...,
3636
rotation: float | Literal["vertical", "horizontal"] | None = ...,
37-
linespacing: float | None = ...,
37+
linespacing: Literal["normal"] | float | None = ...,
3838
rotation_mode: Literal["default", "anchor"] | None = ...,
3939
usetex: bool | None = ...,
4040
wrap: bool = ...,
@@ -79,7 +79,8 @@ class Text(Artist):
7979
self, align: Literal["left", "center", "right"]
8080
) -> None: ...
8181
def set_multialignment(self, align: Literal["left", "center", "right"]) -> None: ...
82-
def set_linespacing(self, spacing: float) -> None: ...
82+
def set_linespacing(self, spacing: Literal["normal"] | float) -> None: ...
83+
def get_linespacing(self) -> Literal["normal"] | float: ...
8384
def set_fontfamily(self, fontname: str | Iterable[str]) -> None: ...
8485
def set_fontfeatures(self, features: Sequence[str] | None) -> None: ...
8586
def set_fontvariant(self, variant: Literal["normal", "small-caps"]) -> None: ...

0 commit comments

Comments
 (0)