Skip to content

Commit 1fe90ab

Browse files
committed
Fix geom_text & geom_label with adjust_text
Responding to adjustText>=1.0.0
1 parent 0b147f0 commit 1fe90ab

File tree

6 files changed

+20
-7
lines changed

6 files changed

+20
-7
lines changed

doc/changelog.qmd

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ title: Changelog
77

88
### Bug Fixes
99

10-
- Fix bug in [](:class:`~plotnine.stat_smooth`) where you could not set the
10+
- Fixed bug in [](:class:`~plotnine.stat_smooth`) where you could not set the
1111
family when using a `glm`. ({{< issue 769 >}})
1212

13-
- Fix bug in [](:class:`~plotnine.position_dodge2`) the widths of the dodged
14-
objects were mixed up. ({{< issue 772 >}})
13+
- Fixed bug in [](:class:`~plotnine.position_dodge2`) the widths of the
14+
dodged objects were mixed up. ({{< issue 772 >}})
15+
16+
- Fixed [](:class:`~plotnine.geom_text`) to work better with
17+
[adjustText](https://pypi.org/project/adjustText/) `v1.0.0` and above.
1518

1619
### Enhancements
1720

plotnine/geoms/geom_text.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ def draw_group(
252252
if params["path_effects"]:
253253
text_elem.set_path_effects(params["path_effects"])
254254

255-
_adjust = params["adjust_text"]
256-
if _adjust:
255+
# TODO: Do adjust text per panel
256+
if _adjust := params["adjust_text"]:
257257
from adjustText import adjust_text
258258

259259
if params["zorder"] == 1:
@@ -263,10 +263,20 @@ def draw_group(
263263
PlotnineWarning,
264264
)
265265

266-
arrowprops = _adjust.pop("arrowprops", {})
266+
_adjust = _adjust.copy()
267+
arrowprops = _adjust.pop("arrowprops", {}).copy()
267268
if "color" not in arrowprops:
268269
arrowprops["color"] = color[0]
269270

271+
# The head_length, tail_length and tail_width of the arrow are
272+
# specified on the same scale as the fontsize, but their default
273+
# values are in the [0, 1] range. The true values are obtained by
274+
# multiplying by the mutation_scale. The default value of
275+
# mutation_scale is 1, so the arrow is effectively invisible.
276+
# A good default for this usecase is the size of text.
277+
if "mutation_scale" not in arrowprops:
278+
arrowprops["mutation_scale"] = data["size"].mean()
279+
270280
adjust_text(texts, ax=ax, arrowprops=arrowprops, **_adjust)
271281

272282
@staticmethod
Loading
Loading
Loading

tests/test_geom_text_label.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
)
4040

4141
adjust_text = {
42-
"expand_points": (2, 2),
42+
"expand": (2, 2),
4343
"arrowprops": {"arrowstyle": "->", "color": "red"},
4444
}
4545

0 commit comments

Comments
 (0)