Skip to content

Commit 40c77d3

Browse files
authored
Merge pull request #21853 from StefRe/fix/fancyarrow_demo
DOC: Fix Annotation arrow style reference example
2 parents 30f1c94 + c84f051 commit 40c77d3

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

examples/text_labels_and_annotations/fancyarrow_demo.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"""
88

99
import inspect
10+
import re
11+
import itertools
1012

1113
import matplotlib.patches as mpatches
1214
import matplotlib.pyplot as plt
@@ -16,19 +18,19 @@
1618
nrow = (len(styles) + 1) // ncol
1719
axs = (plt.figure(figsize=(4 * ncol, 1 + nrow))
1820
.add_gridspec(1 + nrow, ncol,
19-
wspace=.5, left=.1, right=.9, bottom=0, top=1).subplots())
21+
wspace=.7, left=.1, right=.9, bottom=0, top=1).subplots())
2022
for ax in axs.flat:
2123
ax.set_axis_off()
2224
for ax in axs[0, :]:
2325
ax.text(0, .5, "arrowstyle",
2426
transform=ax.transAxes, size="large", color="tab:blue",
2527
horizontalalignment="center", verticalalignment="center")
26-
ax.text(.5, .5, "default parameters",
28+
ax.text(.35, .5, "default parameters",
2729
transform=ax.transAxes,
2830
horizontalalignment="left", verticalalignment="center")
2931
for ax, (stylename, stylecls) in zip(axs[1:, :].T.flat, styles.items()):
30-
l, = ax.plot(.35, .5, "ok", transform=ax.transAxes)
31-
ax.annotate(stylename, (.35, .5), (0, .5),
32+
l, = ax.plot(.25, .5, "ok", transform=ax.transAxes)
33+
ax.annotate(stylename, (.25, .5), (-0.1, .5),
3234
xycoords="axes fraction", textcoords="axes fraction",
3335
size="large", color="tab:blue",
3436
horizontalalignment="center", verticalalignment="center",
@@ -37,8 +39,12 @@
3739
color="k", shrinkA=5, shrinkB=5, patchB=l,
3840
),
3941
bbox=dict(boxstyle="square", fc="w"))
40-
ax.text(.5, .5,
41-
str(inspect.signature(stylecls))[1:-1].replace(", ", "\n"),
42+
# wrap at every nth comma (n = 1 or 2, depending on text length)
43+
s = str(inspect.signature(stylecls))[1:-1]
44+
n = 2 if s.count(',') > 3 else 1
45+
ax.text(.35, .5,
46+
re.sub(', ', lambda m, c=itertools.count(1): m.group()
47+
if next(c) % n else '\n', s),
4248
transform=ax.transAxes,
4349
horizontalalignment="left", verticalalignment="center")
4450

0 commit comments

Comments
 (0)