|
27 | 27 | size=fontsize,
|
28 | 28 | bbox=dict(boxstyle=stylename, fc="w", ec="k"))
|
29 | 29 |
|
30 |
| -plt.show() |
31 | 30 |
|
32 | 31 | ###############################################################################
|
33 | 32 | # Next we'll show off multiple fancy boxes at once.
|
34 | 33 |
|
35 |
| -# Bbox object around which the fancy box will be drawn. |
36 |
| -bb = mtransforms.Bbox([[0.3, 0.4], [0.7, 0.6]]) |
37 |
| - |
38 |
| - |
39 |
| -def draw_bbox(ax, bb): |
40 |
| - # boxstyle=square with pad=0, i.e. bbox itself. |
41 |
| - p_bbox = FancyBboxPatch((bb.xmin, bb.ymin), |
42 |
| - abs(bb.width), abs(bb.height), |
43 |
| - boxstyle="square,pad=0.", |
44 |
| - ec="k", fc="none", zorder=10., |
45 |
| - ) |
46 |
| - ax.add_patch(p_bbox) |
47 |
| - |
48 |
| - |
49 |
| -def test1(ax): |
50 |
| - |
51 |
| - # a fancy box with round corners. pad=0.1 |
52 |
| - p_fancy = FancyBboxPatch((bb.xmin, bb.ymin), |
53 |
| - abs(bb.width), abs(bb.height), |
54 |
| - boxstyle="round,pad=0.1", |
55 |
| - fc=(1., .8, 1.), |
56 |
| - ec=(1., 0.5, 1.)) |
57 |
| - |
58 |
| - ax.add_patch(p_fancy) |
59 |
| - |
60 |
| - ax.text(0.1, 0.8, |
61 |
| - r' boxstyle="round,pad=0.1"', |
62 |
| - size=10, transform=ax.transAxes) |
63 |
| - |
64 |
| - # draws control points for the fancy box. |
65 |
| - # l = p_fancy.get_path().vertices |
66 |
| - # ax.plot(l[:,0], l[:,1], ".") |
67 |
| - |
68 |
| - # draw the original bbox in black |
69 |
| - draw_bbox(ax, bb) |
70 |
| - |
71 |
| - |
72 |
| -def test2(ax): |
73 |
| - |
74 |
| - # bbox=round has two optional argument. pad and rounding_size. |
75 |
| - # They can be set during the initialization. |
76 |
| - p_fancy = FancyBboxPatch((bb.xmin, bb.ymin), |
77 |
| - abs(bb.width), abs(bb.height), |
78 |
| - boxstyle="round,pad=0.1", |
79 |
| - fc=(1., .8, 1.), |
80 |
| - ec=(1., 0.5, 1.)) |
81 |
| - |
82 |
| - ax.add_patch(p_fancy) |
83 |
| - |
84 |
| - # boxstyle and its argument can be later modified with |
85 |
| - # set_boxstyle method. Note that the old attributes are simply |
86 |
| - # forgotten even if the boxstyle name is same. |
87 |
| - |
88 |
| - p_fancy.set_boxstyle("round,pad=0.1, rounding_size=0.2") |
89 |
| - # or |
90 |
| - # p_fancy.set_boxstyle("round", pad=0.1, rounding_size=0.2) |
91 |
| - |
92 |
| - ax.text(0.1, 0.8, |
93 |
| - ' boxstyle="round,pad=0.1\n rounding_size=0.2"', |
94 |
| - size=10, transform=ax.transAxes) |
95 |
| - |
96 |
| - # draws control points for the fancy box. |
97 |
| - # l = p_fancy.get_path().vertices |
98 |
| - # ax.plot(l[:,0], l[:,1], ".") |
99 | 34 |
|
100 |
| - draw_bbox(ax, bb) |
| 35 | +def add_fancy_patch_around(ax, bb, **kwargs): |
| 36 | + fancy = FancyBboxPatch((bb.xmin, bb.ymin), bb.width, bb.height, |
| 37 | + fc=(1, 0.8, 1, 0.5), ec=(1, 0.5, 1, 0.5), |
| 38 | + **kwargs) |
| 39 | + ax.add_patch(fancy) |
| 40 | + return fancy |
101 | 41 |
|
102 | 42 |
|
103 |
| -def test3(ax): |
| 43 | +def draw_control_points_for_patches(ax): |
| 44 | + for patch in ax.patches: |
| 45 | + patch.axes.plot(*patch.get_path().vertices.T, ".", |
| 46 | + c=patch.get_edgecolor()) |
104 | 47 |
|
105 |
| - # mutation_scale determine overall scale of the mutation, |
106 |
| - # i.e. both pad and rounding_size is scaled according to this |
107 |
| - # value. |
108 |
| - p_fancy = FancyBboxPatch((bb.xmin, bb.ymin), |
109 |
| - abs(bb.width), abs(bb.height), |
110 |
| - boxstyle="round,pad=0.1", |
111 |
| - mutation_scale=2., |
112 |
| - fc=(1., .8, 1.), |
113 |
| - ec=(1., 0.5, 1.)) |
114 | 48 |
|
115 |
| - ax.add_patch(p_fancy) |
| 49 | +fig, axs = plt.subplots(2, 2, figsize=(8, 8)) |
116 | 50 |
|
117 |
| - ax.text(0.1, 0.8, |
118 |
| - ' boxstyle="round,pad=0.1"\n mutation_scale=2', |
119 |
| - size=10, transform=ax.transAxes) |
120 |
| - |
121 |
| - # draws control points for the fancy box. |
122 |
| - # l = p_fancy.get_path().vertices |
123 |
| - # ax.plot(l[:,0], l[:,1], ".") |
124 |
| - |
125 |
| - draw_bbox(ax, bb) |
126 |
| - |
127 |
| - |
128 |
| -def test4(ax): |
129 |
| - |
130 |
| - # When the aspect ratio of the axes is not 1, the fancy box may |
131 |
| - # not be what you expected (green) |
132 |
| - |
133 |
| - p_fancy = FancyBboxPatch((bb.xmin, bb.ymin), |
134 |
| - abs(bb.width), abs(bb.height), |
135 |
| - boxstyle="round,pad=0.2", |
136 |
| - fc="none", |
137 |
| - ec=(0., .5, 0.), zorder=4) |
138 |
| - |
139 |
| - ax.add_patch(p_fancy) |
140 |
| - |
141 |
| - # You can compensate this by setting the mutation_aspect (pink). |
142 |
| - p_fancy = FancyBboxPatch((bb.xmin, bb.ymin), |
143 |
| - abs(bb.width), abs(bb.height), |
144 |
| - boxstyle="round,pad=0.3", |
145 |
| - mutation_aspect=.5, |
146 |
| - fc=(1., 0.8, 1.), |
147 |
| - ec=(1., 0.5, 1.)) |
148 |
| - |
149 |
| - ax.add_patch(p_fancy) |
150 |
| - |
151 |
| - ax.text(0.1, 0.8, |
152 |
| - ' boxstyle="round,pad=0.3"\n mutation_aspect=.5', |
153 |
| - size=10, transform=ax.transAxes) |
154 |
| - |
155 |
| - draw_bbox(ax, bb) |
156 |
| - |
157 |
| - |
158 |
| -def test_all(): |
159 |
| - plt.clf() |
160 |
| - |
161 |
| - ax = plt.subplot(2, 2, 1) |
162 |
| - test1(ax) |
163 |
| - ax.set_xlim(0., 1.) |
164 |
| - ax.set_ylim(0., 1.) |
165 |
| - ax.set_title("test1") |
166 |
| - ax.set_aspect(1.) |
167 |
| - |
168 |
| - ax = plt.subplot(2, 2, 2) |
169 |
| - ax.set_title("test2") |
170 |
| - test2(ax) |
171 |
| - ax.set_xlim(0., 1.) |
172 |
| - ax.set_ylim(0., 1.) |
173 |
| - ax.set_aspect(1.) |
174 |
| - |
175 |
| - ax = plt.subplot(2, 2, 3) |
176 |
| - ax.set_title("test3") |
177 |
| - test3(ax) |
178 |
| - ax.set_xlim(0., 1.) |
179 |
| - ax.set_ylim(0., 1.) |
180 |
| - ax.set_aspect(1) |
181 |
| - |
182 |
| - ax = plt.subplot(2, 2, 4) |
183 |
| - ax.set_title("test4") |
184 |
| - test4(ax) |
185 |
| - ax.set_xlim(-0.5, 1.5) |
186 |
| - ax.set_ylim(0., 1.) |
187 |
| - ax.set_aspect(2.) |
| 51 | +# Bbox object around which the fancy box will be drawn. |
| 52 | +bb = mtransforms.Bbox([[0.3, 0.4], [0.7, 0.6]]) |
188 | 53 |
|
189 |
| - plt.show() |
| 54 | +ax = axs[0, 0] |
| 55 | +# a fancy box with round corners. pad=0.1 |
| 56 | +fancy = add_fancy_patch_around(ax, bb, boxstyle="round,pad=0.1") |
| 57 | +ax.set(xlim=(0, 1), ylim=(0, 1), aspect=1, |
| 58 | + title='boxstyle="round,pad=0.1"') |
| 59 | + |
| 60 | +ax = axs[0, 1] |
| 61 | +# bbox=round has two optional arguments: pad and rounding_size. |
| 62 | +# They can be set during the initialization. |
| 63 | +fancy = add_fancy_patch_around(ax, bb, boxstyle="round,pad=0.1") |
| 64 | +# The boxstyle and its argument can be later modified with set_boxstyle(). |
| 65 | +# Note that the old attributes are simply forgotten even if the boxstyle name |
| 66 | +# is same. |
| 67 | +fancy.set_boxstyle("round,pad=0.1,rounding_size=0.2") |
| 68 | +# or: fancy.set_boxstyle("round", pad=0.1, rounding_size=0.2) |
| 69 | +ax.set(xlim=(0, 1), ylim=(0, 1), aspect=1, |
| 70 | + title='boxstyle="round,pad=0.1,rounding_size=0.2"') |
| 71 | + |
| 72 | +ax = axs[1, 0] |
| 73 | +# mutation_scale determines the overall scale of the mutation, i.e. both pad |
| 74 | +# and rounding_size is scaled according to this value. |
| 75 | +fancy = add_fancy_patch_around( |
| 76 | + ax, bb, boxstyle="round,pad=0.1", mutation_scale=2) |
| 77 | +ax.set(xlim=(0, 1), ylim=(0, 1), aspect=1, |
| 78 | + title='boxstyle="round,pad=0.1"\n mutation_scale=2') |
| 79 | + |
| 80 | +ax = axs[1, 1] |
| 81 | +# When the aspect ratio of the axes is not 1, the fancy box may not be what you |
| 82 | +# expected (green). |
| 83 | +fancy = add_fancy_patch_around(ax, bb, boxstyle="round,pad=0.2") |
| 84 | +fancy.set(facecolor="none", edgecolor="green") |
| 85 | +# You can compensate this by setting the mutation_aspect (pink). |
| 86 | +fancy = add_fancy_patch_around( |
| 87 | + ax, bb, boxstyle="round,pad=0.3", mutation_aspect=0.5) |
| 88 | +ax.set(xlim=(-.5, 1.5), ylim=(0, 1), aspect=2, |
| 89 | + title='boxstyle="round,pad=0.3"\nmutation_aspect=.5') |
| 90 | + |
| 91 | +for ax in axs.flat: |
| 92 | + draw_control_points_for_patches(ax) |
| 93 | + # Draw the original bbox (using boxstyle=square with pad=0). |
| 94 | + fancy = add_fancy_patch_around(ax, bb, boxstyle="square,pad=0") |
| 95 | + fancy.set(edgecolor="black", facecolor="none", zorder=10) |
| 96 | + |
| 97 | +fig.tight_layout() |
190 | 98 |
|
191 | 99 |
|
192 |
| -test_all() |
| 100 | +plt.show() |
193 | 101 |
|
194 | 102 | #############################################################################
|
195 | 103 | #
|
|
0 commit comments