Skip to content

Commit 4530d4c

Browse files
committed
Fix adjust_text
1 parent 0b147f0 commit 4530d4c

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

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: 4 additions & 4 deletions
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

@@ -88,7 +88,7 @@ def test_label_aesthetics():
8888
assert p == "label_aesthetics"
8989

9090

91-
@pytest.mark.skipif(is_CI, reason="Sudden small difference on GHA")
91+
# @pytest.mark.skipif(is_CI, reason="Sudden small difference on GHA")
9292
def test_adjust_text():
9393
p = (
9494
ggplot(mtcars.tail(2), aes("mpg", "disp", label="name"))
@@ -98,7 +98,7 @@ def test_adjust_text():
9898
assert p == "adjust_text"
9999

100100

101-
@pytest.mark.skipif(is_CI, reason="Sudden small difference on GHA")
101+
# @pytest.mark.skipif(is_CI, reason="Sudden small difference on GHA")
102102
def test_adjust_label():
103103
p = (
104104
ggplot(mtcars.tail(2), aes("mpg", "disp", label="name"))
@@ -108,7 +108,7 @@ def test_adjust_label():
108108
assert p == "adjust_label"
109109

110110

111-
@pytest.mark.skipif(is_CI, reason="Sudden small difference on GHA")
111+
# @pytest.mark.skipif(is_CI, reason="Sudden small difference on GHA")
112112
def test_adjust_text_default_color():
113113
adjust_text2 = adjust_text.copy()
114114
del adjust_text2["arrowprops"]["color"]

0 commit comments

Comments
 (0)