3
3
Drawing fancy boxes
4
4
===================
5
5
6
- The following examples show how to plot boxes with different visual properties.
6
+ The following examples show how to plot boxes (`.FancyBboxPatch`) with different
7
+ visual properties.
7
8
"""
8
9
9
10
import inspect
15
16
import matplotlib .transforms as mtransforms
16
17
17
18
# %%
18
- # First we'll show some sample boxes with fancybox.
19
+ # Box styles
20
+ # ----------
21
+ # `.FancyBboxPatch` supports different `.BoxStyle`\s. Note that `~.Axes.text`
22
+ # allows to draw a box around the text by adding the ``bbox`` parameter. Therefore,
23
+ # you don't see explicit `.FancyBboxPatch` and `.BoxStyle` calls in the following
24
+ # example.
19
25
20
26
styles = mpatch .BoxStyle .get_styles ()
21
27
ncol = 2
41
47
42
48
43
49
# %%
44
- # Next we'll show off multiple fancy boxes at once.
45
-
50
+ # Parameters for modifying the box
51
+ # --------------------------------
52
+ # `.BoxStyle`\s have additional parameters to configure their appearance.
53
+ # For example, "round" boxes can have ``pad`` and ``rounding``.
54
+ #
55
+ # Additionally, the `.FancyBboxPatch` parameters ``mutation_scale`` and
56
+ # ``mutation_aspect`` scale the box appearance.
46
57
47
58
def add_fancy_patch_around (ax , bb , ** kwargs ):
48
- fancy = FancyBboxPatch (bb .p0 , bb .width , bb .height ,
49
- fc = (1 , 0.8 , 1 , 0.5 ), ec = (1 , 0.5 , 1 , 0.5 ),
50
- ** kwargs )
59
+ kwargs = {
60
+ 'facecolor' : (1 , 0.8 , 1 , 0.5 ),
61
+ 'edgecolor' : (1 , 0.5 , 1 , 0.5 ),
62
+ ** kwargs
63
+ }
64
+ fancy = FancyBboxPatch (bb .p0 , bb .width , bb .height , ** kwargs )
51
65
ax .add_patch (fancy )
52
66
return fancy
53
67
@@ -65,7 +79,7 @@ def draw_control_points_for_patches(ax):
65
79
66
80
ax = axs [0 , 0 ]
67
81
# a fancy box with round corners. pad=0.1
68
- fancy = add_fancy_patch_around (ax , bb , boxstyle = "round,pad=0.1" )
82
+ add_fancy_patch_around (ax , bb , boxstyle = "round,pad=0.1" )
69
83
ax .set (xlim = (0 , 1 ), ylim = (0 , 1 ), aspect = 1 ,
70
84
title = 'boxstyle="round,pad=0.1"' )
71
85
@@ -84,33 +98,61 @@ def draw_control_points_for_patches(ax):
84
98
ax = axs [1 , 0 ]
85
99
# mutation_scale determines the overall scale of the mutation, i.e. both pad
86
100
# and rounding_size is scaled according to this value.
87
- fancy = add_fancy_patch_around (
88
- ax , bb , boxstyle = "round,pad=0.1" , mutation_scale = 2 )
101
+ add_fancy_patch_around (ax , bb , boxstyle = "round,pad=0.1" , mutation_scale = 2 )
89
102
ax .set (xlim = (0 , 1 ), ylim = (0 , 1 ), aspect = 1 ,
90
103
title = 'boxstyle="round,pad=0.1"\n mutation_scale=2' )
91
104
92
105
ax = axs [1 , 1 ]
93
- # When the aspect ratio of the Axes is not 1, the fancy box may not be what you
94
- # expected (green).
95
- fancy = add_fancy_patch_around (ax , bb , boxstyle = "round,pad=0.2" )
96
- fancy .set (facecolor = "none" , edgecolor = "green" )
97
- # You can compensate this by setting the mutation_aspect (pink).
98
- fancy = add_fancy_patch_around (
99
- ax , bb , boxstyle = "round,pad=0.3" , mutation_aspect = 0.5 )
100
- ax .set (xlim = (- .5 , 1.5 ), ylim = (0 , 1 ), aspect = 2 ,
101
- title = 'boxstyle="round,pad=0.3"\n mutation_aspect=.5' )
106
+ # mutation_aspect scales the vertical influence of the parameters (technically,
107
+ # it scales the height of the box down by mutation_aspect, applies the box parameters
108
+ # and scales the result back up). In effect, the vertical pad is scaled to
109
+ # pad * mutation_aspect, e.g. mutation_aspect=0.5 halves the vertical pad.
110
+ add_fancy_patch_around (ax , bb , boxstyle = "round,pad=0.1" , mutation_aspect = 0.5 )
111
+ ax .set (xlim = (0 , 1 ), ylim = (0 , 1 ),
112
+ title = 'boxstyle="round,pad=0.1"\n mutation_aspect=0.5' )
102
113
103
114
for ax in axs .flat :
104
115
draw_control_points_for_patches (ax )
105
116
# Draw the original bbox (using boxstyle=square with pad=0).
106
- fancy = add_fancy_patch_around (ax , bb , boxstyle = "square,pad=0" )
107
- fancy . set ( edgecolor = "black" , facecolor = "none" , zorder = 10 )
117
+ add_fancy_patch_around (ax , bb , boxstyle = "square,pad=0" ,
118
+ edgecolor = "black" , facecolor = "none" , zorder = 10 )
108
119
109
120
fig .tight_layout ()
110
121
111
122
112
123
plt .show ()
113
124
125
+ # %%
126
+ # Creating visually constant padding on non-equal aspect Axes
127
+ # -----------------------------------------------------------
128
+ # Since padding is in box coordinates, i.e. usually data coordinates,
129
+ # a given padding is rendered to different visual sizes if the
130
+ # Axes aspect is not 1.
131
+ # To get visually equal vertical and horizontal padding, set the
132
+ # mutation_aspect to the inverse of the Axes aspect. This scales
133
+ # the vertical padding appropriately.
134
+
135
+ fig , (ax1 , ax2 ) = plt .subplots (1 , 2 , figsize = (6.5 , 5 ))
136
+
137
+ # original boxes
138
+ bb = mtransforms .Bbox ([[- 0.5 , - 0.5 ], [0.5 , 0.5 ]])
139
+ add_fancy_patch_around (ax1 , bb , boxstyle = "square,pad=0" ,
140
+ edgecolor = "black" , facecolor = "none" , zorder = 10 )
141
+ add_fancy_patch_around (ax2 , bb , boxstyle = "square,pad=0" ,
142
+ edgecolor = "black" , facecolor = "none" , zorder = 10 )
143
+ ax1 .set (xlim = (- 1.5 , 1.5 ), ylim = (- 1.5 , 1.5 ), aspect = 2 )
144
+ ax2 .set (xlim = (- 1.5 , 1.5 ), ylim = (- 1.5 , 1.5 ), aspect = 2 )
145
+
146
+
147
+ fancy = add_fancy_patch_around (
148
+ ax1 , bb , boxstyle = "round,pad=0.5" )
149
+ ax1 .set_title ("aspect=2\n mutation_aspect=1" )
150
+
151
+ fancy = add_fancy_patch_around (
152
+ ax2 , bb , boxstyle = "round,pad=0.5" , mutation_aspect = 0.5 )
153
+ ax2 .set_title ("aspect=2\n mutation_aspect=0.5" )
154
+
155
+
114
156
# %%
115
157
#
116
158
# .. admonition:: References
0 commit comments