Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 99b6c1e

Browse files
authored
Merge pull request #21102 from meeseeksmachine/auto-backport-of-pr-21100-on-v3.5.x
Backport PR #21100 on branch v3.5.x (Cleanup demo_tight_layout.)
2 parents c1f6837 + 94b421e commit 99b6c1e

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

examples/subplots_axes_and_figures/demo_tight_layout.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def example_plot(ax):
3131

3232
fig, ax = plt.subplots()
3333
example_plot(ax)
34-
plt.tight_layout()
34+
fig.tight_layout()
3535

3636
###############################################################################
3737

@@ -40,61 +40,53 @@ def example_plot(ax):
4040
example_plot(ax2)
4141
example_plot(ax3)
4242
example_plot(ax4)
43-
plt.tight_layout()
43+
fig.tight_layout()
4444

4545
###############################################################################
4646

4747
fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)
4848
example_plot(ax1)
4949
example_plot(ax2)
50-
plt.tight_layout()
50+
fig.tight_layout()
5151

5252
###############################################################################
5353

5454
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
5555
example_plot(ax1)
5656
example_plot(ax2)
57-
plt.tight_layout()
57+
fig.tight_layout()
5858

5959
###############################################################################
6060

6161
fig, axs = plt.subplots(nrows=3, ncols=3)
6262
for ax in axs.flat:
6363
example_plot(ax)
64-
plt.tight_layout()
64+
fig.tight_layout()
6565

6666
###############################################################################
6767

68-
fig = plt.figure()
69-
68+
plt.figure()
7069
ax1 = plt.subplot(221)
7170
ax2 = plt.subplot(223)
7271
ax3 = plt.subplot(122)
73-
7472
example_plot(ax1)
7573
example_plot(ax2)
7674
example_plot(ax3)
77-
7875
plt.tight_layout()
7976

8077
###############################################################################
8178

82-
fig = plt.figure()
83-
79+
plt.figure()
8480
ax1 = plt.subplot2grid((3, 3), (0, 0))
8581
ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2)
8682
ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)
8783
ax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
88-
8984
example_plot(ax1)
9085
example_plot(ax2)
9186
example_plot(ax3)
9287
example_plot(ax4)
93-
9488
plt.tight_layout()
9589

96-
plt.show()
97-
9890
###############################################################################
9991

10092
fig = plt.figure()
@@ -103,20 +95,16 @@ def example_plot(ax):
10395
ax1 = fig.add_subplot(gs1[0])
10496
ax2 = fig.add_subplot(gs1[1])
10597
ax3 = fig.add_subplot(gs1[2])
106-
10798
example_plot(ax1)
10899
example_plot(ax2)
109100
example_plot(ax3)
110-
111101
gs1.tight_layout(fig, rect=[None, None, 0.45, None])
112102

113103
gs2 = fig.add_gridspec(2, 1)
114104
ax4 = fig.add_subplot(gs2[0])
115105
ax5 = fig.add_subplot(gs2[1])
116-
117106
example_plot(ax4)
118107
example_plot(ax5)
119-
120108
with warnings.catch_warnings():
121109
# gs2.tight_layout cannot handle the subplots from the first gridspec
122110
# (gs1), so it will raise a warning. We are going to match the gridspecs

0 commit comments

Comments
 (0)