Skip to content

Commit 69a1b7f

Browse files
committed
fix(themes): adapt linespacing across the matplotlib 3.11 boundary
The 3.11 text overhaul changes how line height is derived from the font, so the previous numeric linespacing values no longer render consistently. Select per-version linespacing ("normal" on >=3.11, the prior numeric values otherwise) in theme_gray, theme_matplotlib, theme_seaborn, and theme_void. Also add a CI step that downgrades matplotlib below 3.11 so the lower bound stays tested.
1 parent 406f6b9 commit 69a1b7f

5 files changed

Lines changed: 29 additions & 6 deletions

File tree

.github/workflows/testing.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ jobs:
4242
# Install as an editable so that the coverage path
4343
# is predicable
4444
uv run uv pip install --resolution=${{ matrix.resolution }} -e ".[extra,test]"
45+
uv lock --upgrade-package "matplotlib<3.11.0"
4546
4647
- name: Environment Information
4748
run: uv pip list

plotnine/themes/theme_gray.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class theme_gray(theme):
2727
"""
2828

2929
def __init__(self, base_size=11, base_family=None):
30+
import matplotlib as mpl
31+
from packaging import version
32+
33+
MPL311 = version.parse(mpl.__version__) >= version.parse("3.11.0")
34+
ls1, ls2 = ("normal", None) if MPL311 else (0.9, 1.5)
35+
3036
base_family = base_family or get_option("base_family")
3137
half_line = base_size / 2
3238
quarter_line = base_size / 4
@@ -47,7 +53,7 @@ def __init__(self, base_size=11, base_family=None):
4753
color="black",
4854
ma="center",
4955
size=base_size,
50-
linespacing=0.9,
56+
linespacing=ls1, # pyright: ignore[reportArgumentType]
5157
rotation=0,
5258
margin=margin(),
5359
),
@@ -137,7 +143,7 @@ def __init__(self, base_size=11, base_family=None):
137143
strip_text=element_text(
138144
color="#1A1A1A",
139145
size=base_size * 0.8,
140-
linespacing=1.5,
146+
linespacing=ls2,
141147
margin=margin_auto(half_line * 0.8),
142148
),
143149
strip_text_y=element_text(rotation=-90),

plotnine/themes/theme_matplotlib.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class theme_matplotlib(theme):
3030

3131
def __init__(self, rc=None, fname=None, use_defaults=True):
3232
import matplotlib as mpl
33+
from packaging import version
34+
35+
MPL311 = version.parse(mpl.__version__) >= version.parse("3.11.0")
36+
ls1, ls2 = ("normal", None) if MPL311 else (1, 1.5)
3337

3438
m = get_option("base_margin")
3539
base_size = mpl.rcParams.get("font.size", 11)
@@ -41,7 +45,7 @@ def __init__(self, rc=None, fname=None, use_defaults=True):
4145
rect=element_rect(size=linewidth),
4246
text=element_text(
4347
size=base_size,
44-
linespacing=1,
48+
linespacing=ls1, # pyright: ignore[reportArgumentType]
4549
rotation=0,
4650
margin={},
4751
),
@@ -111,7 +115,7 @@ def __init__(self, rc=None, fname=None, use_defaults=True):
111115
fill="#D9D9D9", color="black", size=linewidth
112116
),
113117
strip_text=element_text(
114-
linespacing=1.5,
118+
linespacing=ls2,
115119
margin=margin_auto(half_line * 0.8),
116120
),
117121
strip_text_y=element_text(rotation=-90),

plotnine/themes/theme_seaborn.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,14 @@ def __init__(
3939
font="sans-serif",
4040
font_scale=1,
4141
):
42+
import matplotlib as mpl
43+
from packaging import version
44+
4245
from .seaborn_rcmod import set_theme
4346

47+
MPL311 = version.parse(mpl.__version__) >= version.parse("3.11.0")
48+
_, ls2 = ("normal", None) if MPL311 else (1, 1.5)
49+
4450
rcparams = set_theme(
4551
context=context, style=style, font=font, font_scale=font_scale
4652
)
@@ -126,7 +132,7 @@ def __init__(
126132
strip_background=element_rect(color="none", fill="#D1CDDF"),
127133
strip_text=element_text(
128134
size=base_size * 0.8,
129-
linespacing=1.5,
135+
linespacing=ls2,
130136
margin=margin_auto(half_line * 0.8),
131137
),
132138
strip_text_y=element_text(rotation=-90),

plotnine/themes/theme_void.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ class theme_void(theme):
2424
"""
2525

2626
def __init__(self, base_size=11, base_family=None):
27+
import matplotlib as mpl
28+
from packaging import version
29+
30+
MPL311 = version.parse(mpl.__version__) >= version.parse("3.11.0")
31+
ls1, _ = ("normal", None) if MPL311 else (0.9, 1.5)
32+
2733
base_family = base_family or get_option("base_family")
2834
m = get_option("base_margin")
2935
# Use only inherited elements and make everything blank
@@ -36,7 +42,7 @@ def __init__(self, base_size=11, base_family=None):
3642
style="normal",
3743
color="black",
3844
size=base_size,
39-
linespacing=0.9,
45+
linespacing=ls1,
4046
rotation=0,
4147
margin=margin(),
4248
),

0 commit comments

Comments
 (0)