Skip to content

Commit ad69e89

Browse files
authored
Merge pull request #11619 from jklymak/doc-rearrange-gridspec-examples
DOC: move a couple of GridSpec examples into subplots gallery
2 parents a60c37c + aab834c commit ad69e89

File tree

6 files changed

+145
-40
lines changed

6 files changed

+145
-40
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
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
=======================================================
3+
Using Gridspec to make multi-column/row subplot layouts
4+
=======================================================
5+
6+
`.GridSpec` is a flexible way to layout
7+
subplot grids. Here is an example with a 3x3 grid, and
8+
axes spanning all three columns, two columns, and two rows.
9+
10+
"""
11+
import matplotlib.pyplot as plt
12+
from matplotlib.gridspec import GridSpec
13+
14+
15+
def format_axes(fig):
16+
for i, ax in enumerate(fig.axes):
17+
ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
18+
ax.tick_params(labelbottom=False, labelleft=False)
19+
20+
fig = plt.figure(constrained_layout=True)
21+
22+
gs = GridSpec(3, 3, figure=fig)
23+
ax1 = fig.add_subplot(gs[0, :])
24+
# identical to ax1 = plt.subplot(gs.new_subplotspec((0, 0), colspan=3))
25+
ax2 = fig.add_subplot(gs[1, :-1])
26+
ax3 = fig.add_subplot(gs[1:, -1])
27+
ax4 = fig.add_subplot(gs[-1, 0])
28+
ax5 = fig.add_subplot(gs[-1, -2])
29+
30+
fig.suptitle("GridSpec")
31+
format_axes(fig)
32+
33+
plt.show()

examples/userdemo/demo_gridspec04.py renamed to examples/subplots_axes_and_figures/gridspec_nested.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
"""
2-
===============
3-
Demo Gridspec04
4-
===============
2+
================
3+
Nested Gridspecs
4+
================
5+
6+
GridSpecs can be nested, so that a subplot from a parent GridSpec can
7+
set the position for a nested grid of subplots.
58
69
"""
710
import matplotlib.pyplot as plt
811
import matplotlib.gridspec as gridspec
912

1013

11-
def make_ticklabels_invisible(fig):
14+
def format_axes(fig):
1215
for i, ax in enumerate(fig.axes):
1316
ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
1417
ax.tick_params(labelbottom=False, labelleft=False)
1518

1619

1720
# gridspec inside gridspec
18-
1921
f = plt.figure()
2022

21-
gs0 = gridspec.GridSpec(1, 2)
23+
gs0 = gridspec.GridSpec(1, 2, figure=f)
2224

2325
gs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[0])
2426

@@ -40,6 +42,6 @@ def make_ticklabels_invisible(fig):
4042
f.add_subplot(ax6)
4143

4244
plt.suptitle("GridSpec Inside GridSpec")
43-
make_ticklabels_invisible(f)
45+
format_axes(f)
4446

4547
plt.show()

examples/userdemo/demo_gridspec02.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)