Skip to content

Commit aab834c

Browse files
committed
DOC: modify tight_layout and add constrained_lyaout demo
1 parent aa58a36 commit aab834c

File tree

4 files changed

+104
-4
lines changed

4 files changed

+104
-4
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ per-file-ignores =
251251
examples/style_sheets/plot_solarizedlight2.py: E501
252252
examples/subplots_axes_and_figures/axes_margins.py: E402
253253
examples/subplots_axes_and_figures/axes_zoom_effect.py: E402
254+
examples/subplots_axes_and_figures/demo_constrained_layout.py: E402
254255
examples/subplots_axes_and_figures/demo_tight_layout.py: E402
255256
examples/subplots_axes_and_figures/two_scales.py: E402
256257
examples/tests/backend_driver_sgskip.py: E402, E501
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
=====================================
3+
Resizing axes with constrained layout
4+
=====================================
5+
6+
Constrained layout attempts to resize subplots in
7+
a figure so that there are no overlaps between axes objects and labels
8+
on the axes.
9+
10+
See :doc:`/tutorials/intermediate/constrainedlayout_guide` for more details and
11+
:doc:`/tutorials/intermediate/tight_layout_guide` for an alternative.
12+
13+
"""
14+
15+
import matplotlib.pyplot as plt
16+
import itertools
17+
import warnings
18+
19+
20+
def example_plot(ax):
21+
ax.plot([1, 2])
22+
ax.set_xlabel('x-label', fontsize=12)
23+
ax.set_ylabel('y-label', fontsize=12)
24+
ax.set_title('Title', fontsize=14)
25+
26+
27+
###############################################################################
28+
# If we don't use constrained_layout, then labels overlap the axes
29+
30+
fig, axs = plt.subplots(nrows=2, ncols=2, constrained_layout=False)
31+
32+
for ax in axs.flatten():
33+
example_plot(ax)
34+
35+
###############################################################################
36+
# adding ``constrained_layout=True`` automatically adjusts.
37+
38+
fig, axs = plt.subplots(nrows=2, ncols=2, constrained_layout=True)
39+
40+
for ax in axs.flatten():
41+
example_plot(ax)
42+
43+
###############################################################################
44+
# Below is a more complicated example using nested gridspecs.
45+
46+
fig = plt.figure(constrained_layout=True)
47+
48+
import matplotlib.gridspec as gridspec
49+
50+
gs0 = gridspec.GridSpec(1, 2, figure=fig)
51+
52+
gs1 = gridspec.GridSpecFromSubplotSpec(3, 1, subplot_spec=gs0[0])
53+
for n in range(3):
54+
ax = fig.add_subplot(gs1[n])
55+
example_plot(ax)
56+
57+
58+
gs2 = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs0[1])
59+
for n in range(2):
60+
ax = fig.add_subplot(gs2[n])
61+
example_plot(ax)
62+
63+
plt.show()
64+
65+
#############################################################################
66+
#
67+
# ------------
68+
#
69+
# References
70+
# """"""""""
71+
#
72+
# The use of the following functions and methods is shown in this example:
73+
74+
import matplotlib
75+
matplotlib.gridspec.GridSpec
76+
matplotlib.gridspec.GridSpecFromSubplotSpec

examples/subplots_axes_and_figures/demo_tight_layout.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
"""
2-
=================
3-
Demo Tight Layout
4-
=================
2+
===============================
3+
Resizing axes with tight layout
4+
===============================
5+
6+
`~.figure.Figure.tight_layout` attempts to resize subplots in
7+
a figure so that there are no overlaps between axes objects and labels
8+
on the axes.
9+
10+
See :doc:`/tutorials/intermediate/tight_layout_guide` for more details and
11+
:doc:`/tutorials/intermediate/constrainedlayout_guide` for an alternative.
512
613
"""
714

@@ -133,3 +140,19 @@ def example_plot(ax):
133140
gs2.update(top=top, bottom=bottom)
134141

135142
plt.show()
143+
144+
#############################################################################
145+
#
146+
# ------------
147+
#
148+
# References
149+
# """"""""""
150+
#
151+
# The use of the following functions and methods is shown in this example:
152+
153+
import matplotlib
154+
matplotlib.pyplot.tight_layout
155+
matplotlib.figure.Figure.tight_layout
156+
matplotlib.figure.Figure.add_subplot
157+
matplotlib.pyplot.subplot2grid
158+
matplotlib.gridspec.GridSpec

examples/subplots_axes_and_figures/gridspec_multicolumn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Using Gridspec to make multi-column/row subplot layouts
44
=======================================================
55
6-
`~.matplotlib.gridspec.GridSpec` is a flexible way to layout
6+
`.GridSpec` is a flexible way to layout
77
subplot grids. Here is an example with a 3x3 grid, and
88
axes spanning all three columns, two columns, and two rows.
99

0 commit comments

Comments
 (0)