Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ad717f6

Browse filesBrowse files
committed
DOC: Improve fancybox demo
- Create subsections - The plot for the different boxstyle attributes had mixed mutation_scale with Axes aspect. This makes it more difficult to understand. Therefore, we now show the effect of mutation_scale in isoliation. - The aspect-correction topic is separated into an additional plot.
1 parent 13380e1 commit ad717f6
Copy full SHA for ad717f6

File tree

Expand file treeCollapse file tree

1 file changed

+63
-21
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+63
-21
lines changed

‎galleries/examples/shapes_and_collections/fancybox_demo.py

Copy file name to clipboardExpand all lines: galleries/examples/shapes_and_collections/fancybox_demo.py
+63-21Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
Drawing fancy boxes
44
===================
55
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.
78
"""
89

910
import inspect
@@ -15,7 +16,12 @@
1516
import matplotlib.transforms as mtransforms
1617

1718
# %%
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.
1925

2026
styles = mpatch.BoxStyle.get_styles()
2127
ncol = 2
@@ -41,13 +47,21 @@
4147

4248

4349
# %%
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.
4657

4758
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)
5165
ax.add_patch(fancy)
5266
return fancy
5367

@@ -65,7 +79,7 @@ def draw_control_points_for_patches(ax):
6579

6680
ax = axs[0, 0]
6781
# 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")
6983
ax.set(xlim=(0, 1), ylim=(0, 1), aspect=1,
7084
title='boxstyle="round,pad=0.1"')
7185

@@ -84,33 +98,61 @@ def draw_control_points_for_patches(ax):
8498
ax = axs[1, 0]
8599
# mutation_scale determines the overall scale of the mutation, i.e. both pad
86100
# 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)
89102
ax.set(xlim=(0, 1), ylim=(0, 1), aspect=1,
90103
title='boxstyle="round,pad=0.1"\n mutation_scale=2')
91104

92105
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"\nmutation_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"\nmutation_aspect=0.5')
102113

103114
for ax in axs.flat:
104115
draw_control_points_for_patches(ax)
105116
# 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)
108119

109120
fig.tight_layout()
110121

111122

112123
plt.show()
113124

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\nmutation_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\nmutation_aspect=0.5")
154+
155+
114156
# %%
115157
#
116158
# .. admonition:: References

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.