Skip to content

Commit

Permalink
Fix geom_text & geom_label with adjust_text
Browse files Browse the repository at this point in the history
Responding to adjustText>=1.0.0
  • Loading branch information
has2k1 committed Apr 23, 2024
1 parent 0b147f0 commit 1fe90ab
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 7 deletions.
9 changes: 6 additions & 3 deletions doc/changelog.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ title: Changelog

### Bug Fixes

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

- Fix bug in [](:class:`~plotnine.position_dodge2`) the widths of the dodged
objects were mixed up. ({{< issue 772 >}})
- Fixed bug in [](:class:`~plotnine.position_dodge2`) the widths of the
dodged objects were mixed up. ({{< issue 772 >}})

- Fixed [](:class:`~plotnine.geom_text`) to work better with
[adjustText](https://pypi.org/project/adjustText/) `v1.0.0` and above.

### Enhancements

Expand Down
16 changes: 13 additions & 3 deletions plotnine/geoms/geom_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ def draw_group(
if params["path_effects"]:
text_elem.set_path_effects(params["path_effects"])

_adjust = params["adjust_text"]
if _adjust:
# TODO: Do adjust text per panel
if _adjust := params["adjust_text"]:
from adjustText import adjust_text

if params["zorder"] == 1:
Expand All @@ -263,10 +263,20 @@ def draw_group(
PlotnineWarning,
)

arrowprops = _adjust.pop("arrowprops", {})
_adjust = _adjust.copy()
arrowprops = _adjust.pop("arrowprops", {}).copy()
if "color" not in arrowprops:
arrowprops["color"] = color[0]

# The head_length, tail_length and tail_width of the arrow are
# specified on the same scale as the fontsize, but their default
# values are in the [0, 1] range. The true values are obtained by
# multiplying by the mutation_scale. The default value of
# mutation_scale is 1, so the arrow is effectively invisible.
# A good default for this usecase is the size of text.
if "mutation_scale" not in arrowprops:
arrowprops["mutation_scale"] = data["size"].mean()

adjust_text(texts, ax=ax, arrowprops=arrowprops, **_adjust)

@staticmethod
Expand Down
Binary file modified tests/baseline_images/test_geom_text_label/adjust_label.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline_images/test_geom_text_label/adjust_text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/test_geom_text_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
)

adjust_text = {
"expand_points": (2, 2),
"expand": (2, 2),
"arrowprops": {"arrowstyle": "->", "color": "red"},
}

Expand Down

0 comments on commit 1fe90ab

Please sign in to comment.