Compressed layout
Constrained layout typically needs to be activated before any Axes are added to
a figure. Two ways of doing so are
Those are described in detail throughout the following sections.
flat
:
example_plot
(
)
flat
:
example_plot
(
)
=
{
'rasterized'
:
True
,
'cmap'
:
'viridis'
,
'norm'
:
}
,
=
(
figsize
=
(
4
,
4
),
layout
=
"constrained"
)
=
(
,
**
)
(
,
=
,
shrink
=
0.6
)
ax
in
:
=
(
,
**
)
(
,
=
,
shrink
=
0.6
)
ax
in
:
=
(
,
**
)
(
,
=
[
1
:,
1
],
shrink
=
0.8
)
(
,
=
[:,
-
1
],
shrink
=
0.6
)
0
]
.
plot
(
(
10
))
[
1
]
.
plot
(
(
10
),
label
=
'This is a plot'
)
[
1
]
.
legend
(
loc
=
'center left'
,
bbox_to_anchor
=
(
0.8
,
0.5
))
0
]
.
plot
(
(
10
))
[
1
]
.
plot
(
(
10
),
label
=
'This is a plot'
)
=
[
1
]
.
legend
(
loc
=
'center left'
,
bbox_to_anchor
=
(
0.8
,
0.5
))
(
False
)
()
(
True
)
(
'none'
)
try
:
(
'../../../doc/_static/constrained_layout_1b.png'
,
bbox_inches
=
'tight'
,
dpi
=
100
)
except
FileNotFoundError
:
pass
0
]
.
plot
(
(
10
))
=
[
1
]
.
plot
(
(
10
),
label
=
'This is a plot'
)
=
[
l
.
get_label
()
for
l
in
]
=
(
,
,
loc
=
'center left'
,
bbox_to_anchor
=
(
0.8
,
0.5
),
bbox_transform
=
[
1
]
.
transAxes
)
try
:
(
'../../../doc/_static/constrained_layout_2b.png'
,
bbox_inches
=
'tight'
,
dpi
=
100
)
except
FileNotFoundError
:
pass
flat
:
example_plot
(
,
hide_labels
=
True
)
()
.
set
(
w_pad
=
4
/
72
,
h_pad
=
4
/
72
,
hspace
=
0.2
,
wspace
=
0.2
)
flat
:
example_plot
(
,
hide_labels
=
True
)
()
.
set
(
w_pad
=
4
/
72
,
h_pad
=
4
/
72
,
hspace
=
0.2
,
wspace
=
0.2
)
in
:
example_plot
(
,
hide_labels
=
True
)
()
.
set
(
w_pad
=
4
/
72
,
h_pad
=
4
/
72
,
hspace
=
0.0
,
wspace
=
0.0
)
=
[
0
,
0.05
,
0.1
,
0.2
]
for
,
in
zip
(
,
):
=
(
,
**
)
(
,
=
,
shrink
=
0.6
,
=
)
([])
([])
(
f
'pad:
{
}
'
)
()
.
set
(
w_pad
=
2
/
72
,
h_pad
=
2
/
72
,
hspace
=
0.2
,
wspace
=
0.2
)
gs1
=
[
0
]
.
subgridspec
(
2
,
1
)
=
(
[
0
])
=
(
[
1
])
example_plot
(
)
example_plot
(
)
=
[
1
]
.
subgridspec
(
3
,
1
)
for
in
:
=
(
)
example_plot
(
)
(
""
)
(
""
)
(
"x-label"
,
fontsize
=
12
)
6
,
2
)
=
(
[:
3
,
0
])
=
(
[
3
:,
0
])
example_plot
(
)
example_plot
(
)
=
(
[
0
:
2
,
1
])
example_plot
(
,
hide_labels
=
True
)
=
(
[
2
:
4
,
1
])
example_plot
(
,
hide_labels
=
True
)
=
(
[
4
:,
1
])
example_plot
(
,
hide_labels
=
True
)
(
'Overlapping Gridspecs'
)
fig
,
width_ratios
=
[
1
,
2
])
=
[
0
]
.
subgridspec
(
2
,
1
)
=
[
1
]
.
subgridspec
(
2
,
2
)
for
in
:
=
(
)
example_plot
(
)
=
[]
for
in
:
=
(
)
=
(
,
**
)
(
'x-label'
)
(
'y-label'
)
(
'title'
)
+=
[
]
(
'Nested plots using subgridspec'
)
(
,
=
)
,
2
])
=
[
0
]
.
subplots
(
2
,
1
)
for
in
:
example_plot
(
)
=
[
1
]
.
subplots
(
2
,
2
)
for
in
:
=
(
,
**
)
(
'x-label'
)
(
'y-label'
)
(
'title'
)
(
,
=
)
(
'Nested plots using subfigures'
)
=
'compressed'
)
for
in
:
(
)
(
"fixed-aspect plots, layout='compressed'"
)
ax
.
imshow
(
(
10
,
10
),
cmap
=
'viridis'
)
(
"Colorbar with layout='constrained'"
,
fontsize
=
'medium'
)
(
,
=
)
ax
.
imshow
(
(
10
,
10
),
cmap
=
'viridis'
)
(
"Colorbar with layout='compressed'"
,
fontsize
=
'medium'
)
(
,
=
)
ax
.
imshow
(
(
10
,
10
),
cmap
=
'viridis'
)
([
4
,
8
])
(
"Layout='compressed' with zoom"
,
fontsize
=
'medium'
)
(
,
=
)
ax2
=
(
2
,
2
,
3
)
=
(
1
,
2
,
2
)
example_plot
(
)
example_plot
(
)
example_plot
(
)
(
'Mixed nrows, ncols'
)
0
))
=
((
3
,
3
),
(
0
,
1
),
colspan
=
2
)
=
((
3
,
3
),
(
1
,
0
),
colspan
=
2
,
rowspan
=
2
)
=
((
3
,
3
),
(
1
,
2
),
rowspan
=
2
)
example_plot
(
)
example_plot
(
)
example_plot
(
)
example_plot
(
)
(
'subplot2grid'
)
24
)
plot_children
(
)
[
0
],
fontsize
=
32
)
example_plot
(
[
1
],
fontsize
=
8
)
plot_children
(
)
[
0
]
.
pcolormesh
(
,
**
)
(
,
=
[
0
],
shrink
=
0.6
)
=
[
1
]
.
pcolormesh
(
,
**
)
plot_children
(
)
in
:
=
(
,
**
)
(
,
=
,
shrink
=
0.6
)
plot_children
(
)
)
=
(
2
,
2
,
figure
=
)
=
(
[:,
0
])
=
(
,
**
)
=
(
[
0
,
1
])
=
(
,
**
)
=
(
[
1
,
1
])
=
(
,
**
)
plot_children
(
)
One case that requires finessing is if margins do not have any artists
constraining their width. In the case below, the right margin for column 0
and the left margin for column 3 have no margin artists to set their width,
so we take the maximum width of the margin widths that do have artists.
This makes all the Axes have the same size:
Total running time of the script: (0 minutes 31.333 seconds)