Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 46d5a24

Browse files
committed
Show box/arrowstyle parameters in reference examples.
This will allow later removing the table of values in the annotation tutorial. Also, draw things each in their axes and in axes coordinates, to simplify the code.
1 parent 450cdc6 commit 46d5a24

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

examples/shapes_and_collections/fancybox_demo.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
The following examples show how to plot boxes with different visual properties.
77
"""
88

9+
import inspect
10+
911
import matplotlib.pyplot as plt
1012
import matplotlib.transforms as mtransforms
1113
import matplotlib.patches as mpatch
@@ -15,17 +17,16 @@
1517
# First we'll show some sample boxes with fancybox.
1618

1719
styles = mpatch.BoxStyle.get_styles()
18-
spacing = 1.2
19-
20-
figheight = (spacing * len(styles) + .5)
21-
fig = plt.figure(figsize=(4 / 1.5, figheight / 1.5))
22-
fontsize = 0.3 * 72
23-
24-
for i, stylename in enumerate(sorted(styles)):
25-
fig.text(0.5, (spacing * (len(styles) - i) - 0.5) / figheight, stylename,
26-
ha="center",
27-
size=fontsize,
28-
bbox=dict(boxstyle=stylename, fc="w", ec="k"))
20+
ncol = 2
21+
nrow = (len(styles) + 1) // ncol
22+
axs = plt.figure(figsize=(3 * ncol, nrow)).subplots(nrow, ncol)
23+
for ax in axs.flat:
24+
ax.set_axis_off()
25+
for ax, (stylename, stylecls) in zip(axs.T.flat, styles.items()):
26+
ax.text(.5, .5,
27+
f"{stylename}\n{inspect.signature(stylecls)}",
28+
transform=ax.transAxes, ha="center", va="center",
29+
bbox=dict(boxstyle=stylename, fc="w", ec="k"))
2930

3031

3132
###############################################################################

examples/text_labels_and_annotations/fancyarrow_demo.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,32 @@
66
Overview of the arrow styles available in `~.Axes.annotate`.
77
"""
88

9+
import inspect
10+
911
import matplotlib.patches as mpatches
1012
import matplotlib.pyplot as plt
1113

1214
styles = mpatches.ArrowStyle.get_styles()
13-
1415
ncol = 2
1516
nrow = (len(styles) + 1) // ncol
16-
figheight = (nrow + 0.5)
17-
fig = plt.figure(figsize=(4 * ncol / 1.5, figheight / 1.5))
18-
fontsize = 0.2 * 70
19-
20-
ax = fig.add_axes([0, 0, 1, 1], frameon=False)
21-
ax.set(xlim=(0, 4 * ncol), ylim=(0, figheight))
22-
23-
for i, (stylename, styleclass) in enumerate(styles.items()):
24-
x = 3.2 + (i // nrow) * 4
25-
y = (figheight - 0.7 - i % nrow) # /figheight
26-
p = mpatches.Circle((x, y), 0.2)
27-
ax.add_patch(p)
28-
29-
ax.annotate(stylename, (x, y),
30-
(x - 1.2, y),
31-
ha="right", va="center",
32-
size=fontsize,
33-
arrowprops=dict(arrowstyle=stylename,
34-
patchB=p,
35-
shrinkA=5,
36-
shrinkB=5,
37-
fc="k", ec="k",
38-
connectionstyle="arc3,rad=-0.05",
39-
),
17+
axs = plt.figure(
18+
figsize=(4 * ncol, nrow), constrained_layout=True).subplots(nrow, ncol)
19+
for ax in axs.flat:
20+
ax.set_axis_off()
21+
for ax, (stylename, stylecls) in zip(axs.T.flat, styles.items()):
22+
l, = ax.plot(.8, .5, "o", transform=ax.transAxes)
23+
ax.annotate(stylename, (.8, .5), (.4, .5),
24+
xycoords="axes fraction", textcoords="axes fraction",
25+
horizontalalignment="left", verticalalignment="center",
26+
arrowprops=dict(
27+
arrowstyle=stylename, connectionstyle="arc3,rad=-0.05",
28+
color="k", shrinkA=5, shrinkB=5, patchB=l,
29+
),
4030
bbox=dict(boxstyle="square", fc="w"))
31+
ax.text(.35, .5,
32+
str(inspect.signature(stylecls))[1:-1].replace(", ", "\n"),
33+
transform=ax.transAxes,
34+
horizontalalignment="right", verticalalignment="center")
4135

4236
plt.show()
4337

0 commit comments

Comments
 (0)