Fancybox演示

使用Matplotlib绘制精美的盒子。

以下示例显示如何绘制具有不同视觉属性的框。

1
2
3
4
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import matplotlib.patches as mpatch
from matplotlib.patches import FancyBboxPatch

首先,我们将展示一些带有fancybox的样本盒。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
styles = mpatch.BoxStyle.get_styles()
spacing = 1.2

figheight = (spacing * len(styles) + .5)
fig1 = plt.figure(1, (4 / 1.5, figheight / 1.5))
fontsize = 0.3 * 72

for i, stylename in enumerate(sorted(styles)):
fig1.text(0.5, (spacing * (len(styles) - i) - 0.5) / figheight, stylename,
ha="center",
size=fontsize,
transform=fig1.transFigure,
bbox=dict(boxstyle=stylename, fc="w", ec="k"))

plt.show()

Fancybox演示示例

接下来,我们将同时展示多个精美的盒子。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Bbox object around which the fancy box will be drawn.
bb = mtransforms.Bbox([[0.3, 0.4], [0.7, 0.6]])


def draw_bbox(ax, bb):
# boxstyle=square with pad=0, i.e. bbox itself.
p_bbox = FancyBboxPatch((bb.xmin, bb.ymin),
abs(bb.width), abs(bb.height),
boxstyle="square,pad=0.",
ec="k", fc="none", zorder=10.,
)
ax.add_patch(p_bbox)


def test1(ax):

# a fancy box with round corners. pad=0.1
p_fancy = FancyBboxPatch((bb.xmin, bb.ymin),
abs(bb.width), abs(bb.height),
boxstyle="round,pad=0.1",
fc=(1., .8, 1.),
ec=(1., 0.5, 1.))

ax.add_patch(p_fancy)

ax.text(0.1, 0.8,
r' boxstyle="round,pad=0.1"',
size=10, transform=ax.transAxes)

# draws control points for the fancy box.
# l = p_fancy.get_path().vertices
# ax.plot(l[:,0], l[:,1], ".")

# draw the original bbox in black
draw_bbox(ax, bb)


def test2(ax):

# bbox=round has two optional argument. pad and rounding_size.
# They can be set during the initialization.
p_fancy = FancyBboxPatch((bb.xmin, bb.ymin),
abs(bb.width), abs(bb.height),
boxstyle="round,pad=0.1",
fc=(1., .8, 1.),
ec=(1., 0.5, 1.))

ax.add_patch(p_fancy)

# boxstyle and its argument can be later modified with
# set_boxstyle method. Note that the old attributes are simply
# forgotten even if the boxstyle name is same.

p_fancy.set_boxstyle("round,pad=0.1, rounding_size=0.2")
# or
# p_fancy.set_boxstyle("round", pad=0.1, rounding_size=0.2)

ax.text(0.1, 0.8,
' boxstyle="round,pad=0.1\n rounding_size=0.2"',
size=10, transform=ax.transAxes)

# draws control points for the fancy box.
# l = p_fancy.get_path().vertices
# ax.plot(l[:,0], l[:,1], ".")

draw_bbox(ax, bb)


def test3(ax):

# mutation_scale determine overall scale of the mutation,
# i.e. both pad and rounding_size is scaled according to this
# value.
p_fancy = FancyBboxPatch((bb.xmin, bb.ymin),
abs(bb.width), abs(bb.height),
boxstyle="round,pad=0.1",
mutation_scale=2.,
fc=(1., .8, 1.),
ec=(1., 0.5, 1.))

ax.add_patch(p_fancy)

ax.text(0.1, 0.8,
' boxstyle="round,pad=0.1"\n mutation_scale=2',
size=10, transform=ax.transAxes)

# draws control points for the fancy box.
# l = p_fancy.get_path().vertices
# ax.plot(l[:,0], l[:,1], ".")

draw_bbox(ax, bb)


def test4(ax):

# When the aspect ratio of the axes is not 1, the fancy box may
# not be what you expected (green)

p_fancy = FancyBboxPatch((bb.xmin, bb.ymin),
abs(bb.width), abs(bb.height),
boxstyle="round,pad=0.2",
fc="none",
ec=(0., .5, 0.), zorder=4)

ax.add_patch(p_fancy)

# You can compensate this by setting the mutation_aspect (pink).
p_fancy = FancyBboxPatch((bb.xmin, bb.ymin),
abs(bb.width), abs(bb.height),
boxstyle="round,pad=0.3",
mutation_aspect=.5,
fc=(1., 0.8, 1.),
ec=(1., 0.5, 1.))

ax.add_patch(p_fancy)

ax.text(0.1, 0.8,
' boxstyle="round,pad=0.3"\n mutation_aspect=.5',
size=10, transform=ax.transAxes)

draw_bbox(ax, bb)


def test_all():
plt.clf()

ax = plt.subplot(2, 2, 1)
test1(ax)
ax.set_xlim(0., 1.)
ax.set_ylim(0., 1.)
ax.set_title("test1")
ax.set_aspect(1.)

ax = plt.subplot(2, 2, 2)
ax.set_title("test2")
test2(ax)
ax.set_xlim(0., 1.)
ax.set_ylim(0., 1.)
ax.set_aspect(1.)

ax = plt.subplot(2, 2, 3)
ax.set_title("test3")
test3(ax)
ax.set_xlim(0., 1.)
ax.set_ylim(0., 1.)
ax.set_aspect(1)

ax = plt.subplot(2, 2, 4)
ax.set_title("test4")
test4(ax)
ax.set_xlim(-0.5, 1.5)
ax.set_ylim(0., 1.)
ax.set_aspect(2.)

plt.show()


test_all()

Fancybox演示2

参考

此示例中显示了以下函数,方法,类和模块的使用:

1
2
3
4
5
6
import matplotlib
matplotlib.patches
matplotlib.patches.FancyBboxPatch
matplotlib.patches.BoxStyle
matplotlib.patches.BoxStyle.get_styles
matplotlib.transforms.Bbox

下载这个示例