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 618588d

Browse filesBrowse files
committed
DOC: change some examples to use constrained_layout=True
1 parent aae55fc commit 618588d
Copy full SHA for 618588d

File tree

Expand file treeCollapse file tree

6 files changed

+30
-40
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+30
-40
lines changed

‎examples/color/color_cycler.py

Copy file name to clipboardExpand all lines: examples/color/color_cycler.py
+1-3Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
plt.rc('lines', linewidth=4)
2828
plt.rc('axes', prop_cycle=(cycler(color=['r', 'g', 'b', 'y']) +
2929
cycler(linestyle=['-', '--', ':', '-.'])))
30-
fig, (ax0, ax1) = plt.subplots(nrows=2)
30+
fig, (ax0, ax1) = plt.subplots(nrows=2, constrained_layout=True)
3131
ax0.plot(yy)
3232
ax0.set_title('Set default color cycle to rgby')
3333

@@ -41,8 +41,6 @@
4141
ax1.plot(yy)
4242
ax1.set_title('Set axes color cycle to cmyk')
4343

44-
# Tweak spacing between subplots to prevent labels from overlapping
45-
fig.subplots_adjust(hspace=0.3)
4644
plt.show()
4745

4846
#############################################################################

‎examples/images_contours_and_fields/contourf_demo.py

Copy file name to clipboardExpand all lines: examples/images_contours_and_fields/contourf_demo.py
+4-5Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
# occur on nice boundaries, but we do it here for purposes
3939
# of illustration.
4040

41-
fig1, ax2 = plt.subplots()
41+
fig1, ax2 = plt.subplots(constrained_layout=True)
4242
CS = ax2.contourf(X, Y, Z, 10, cmap=plt.cm.bone, origin=origin)
4343

4444
# Note that in the following, we explicitly pass in a subset of
@@ -58,7 +58,7 @@
5858
# Add the contour line levels to the colorbar
5959
cbar.add_lines(CS2)
6060

61-
fig2, ax2 = plt.subplots()
61+
fig2, ax2 = plt.subplots(constrained_layout=True)
6262
# Now make a contour plot with the levels specified,
6363
# and with the colormap generated automatically from a list
6464
# of colors.
@@ -95,12 +95,11 @@
9595
# no effect:
9696
# cmap.set_bad("red")
9797

98-
fig3, axs = plt.subplots(2, 2)
99-
fig3.subplots_adjust(hspace=0.3)
98+
fig, axs = plt.subplots(2, 2, constrained_layout=True)
10099

101100
for ax, extend in zip(axs.ravel(), extends):
102101
cs = ax.contourf(X, Y, Z, levels, cmap=cmap, extend=extend, origin=origin)
103-
fig3.colorbar(cs, ax=ax, shrink=0.9)
102+
fig.colorbar(cs, ax=ax, shrink=0.9)
104103
ax.set_title("extend = %s" % extend)
105104
ax.locator_params(nbins=4)
106105

‎examples/images_contours_and_fields/image_nonuniform.py

Copy file name to clipboardExpand all lines: examples/images_contours_and_fields/image_nonuniform.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525

2626
z = np.sqrt(x[np.newaxis, :]**2 + y[:, np.newaxis]**2)
2727

28-
fig, axs = plt.subplots(nrows=2, ncols=2)
29-
fig.subplots_adjust(bottom=0.07, hspace=0.3)
28+
fig, axs = plt.subplots(nrows=2, ncols=2, constrained_layout=True)
3029
fig.suptitle('NonUniformImage class', fontsize='large')
3130
ax = axs[0, 0]
3231
im = NonUniformImage(ax, interpolation=interp, extent=(-4, 4, -4, 4),

‎examples/lines_bars_and_markers/psd_demo.py

Copy file name to clipboardExpand all lines: examples/lines_bars_and_markers/psd_demo.py
+11-11Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
import matplotlib.pyplot as plt
1313
import numpy as np
1414
import matplotlib.mlab as mlab
15+
import matplotlib.gridspec as gridspec
1516

1617
# Fixing random state for reproducibility
1718
np.random.seed(19680801)
1819

19-
2020
dt = 0.01
2121
t = np.arange(0, 10, dt)
2222
nse = np.random.randn(len(t))
@@ -59,30 +59,32 @@
5959
y = y + np.random.randn(*t.shape)
6060

6161
# Plot the raw time series
62-
fig = plt.figure()
63-
fig.subplots_adjust(hspace=0.45, wspace=0.3)
64-
ax = fig.add_subplot(2, 1, 1)
62+
fig = plt.figure(constrained_layout=True)
63+
gs = gridspec.GridSpec(2, 3, figure=fig)
64+
ax = fig.add_subplot(gs[0, :])
6565
ax.plot(t, y)
66+
ax.set_xlabel('time [s]')
67+
ax.set_ylabel('signal')
6668

6769
# Plot the PSD with different amounts of zero padding. This uses the entire
6870
# time series at once
69-
ax2 = fig.add_subplot(2, 3, 4)
71+
ax2 = fig.add_subplot(gs[1, 0])
7072
ax2.psd(y, NFFT=len(t), pad_to=len(t), Fs=fs)
7173
ax2.psd(y, NFFT=len(t), pad_to=len(t) * 2, Fs=fs)
7274
ax2.psd(y, NFFT=len(t), pad_to=len(t) * 4, Fs=fs)
7375
plt.title('zero padding')
7476

7577
# Plot the PSD with different block sizes, Zero pad to the length of the
7678
# original data sequence.
77-
ax3 = fig.add_subplot(2, 3, 5, sharex=ax2, sharey=ax2)
79+
ax3 = fig.add_subplot(gs[1, 1], sharex=ax2, sharey=ax2)
7880
ax3.psd(y, NFFT=len(t), pad_to=len(t), Fs=fs)
7981
ax3.psd(y, NFFT=len(t) // 2, pad_to=len(t), Fs=fs)
8082
ax3.psd(y, NFFT=len(t) // 4, pad_to=len(t), Fs=fs)
8183
ax3.set_ylabel('')
8284
plt.title('block size')
8385

8486
# Plot the PSD with different amounts of overlap between blocks
85-
ax4 = fig.add_subplot(2, 3, 6, sharex=ax2, sharey=ax2)
87+
ax4 = fig.add_subplot(gs[1, 2], sharex=ax2, sharey=ax2)
8688
ax4.psd(y, NFFT=len(t) // 2, pad_to=len(t), noverlap=0, Fs=fs)
8789
ax4.psd(y, NFFT=len(t) // 2, pad_to=len(t),
8890
noverlap=int(0.05 * len(t) / 2.), Fs=fs)
@@ -106,9 +108,8 @@
106108
xn = (A * np.sin(2 * np.pi * f * t)).sum(axis=0)
107109
xn += 5 * np.random.randn(*t.shape)
108110

109-
fig, (ax0, ax1) = plt.subplots(ncols=2)
111+
fig, (ax0, ax1) = plt.subplots(ncols=2, constrained_layout=True)
110112

111-
fig.subplots_adjust(hspace=0.45, wspace=0.3)
112113
yticks = np.arange(-50, 30, 10)
113114
yrange = (yticks[0], yticks[-1])
114115
xticks = np.arange(0, 550, 100)
@@ -147,9 +148,8 @@
147148
f = np.array([150, 140]).reshape(-1, 1)
148149
xn = (A * np.exp(2j * np.pi * f * t)).sum(axis=0) + 5 * prng.randn(*t.shape)
149150

150-
fig, (ax0, ax1) = plt.subplots(ncols=2)
151+
fig, (ax0, ax1) = plt.subplots(ncols=2, constrained_layout=True)
151152

152-
fig.subplots_adjust(hspace=0.45, wspace=0.3)
153153
yticks = np.arange(-50, 30, 10)
154154
yrange = (yticks[0], yticks[-1])
155155
xticks = np.arange(-500, 550, 200)

‎examples/subplots_axes_and_figures/figure_title.py

Copy file name to clipboardExpand all lines: examples/subplots_axes_and_figures/figure_title.py
+11-14Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,16 @@ def f(t):
2020
t3 = np.arange(0.0, 2.0, 0.01)
2121

2222

23-
plt.subplot(121)
24-
plt.plot(t1, f(t1), 'o', t2, f(t2), '-')
25-
plt.title('subplot 1')
26-
plt.ylabel('Damped oscillation')
27-
plt.suptitle('This is a somewhat long figure title', fontsize=16)
28-
29-
30-
plt.subplot(122)
31-
plt.plot(t3, np.cos(2*np.pi*t3), '--')
32-
plt.xlabel('time (s)')
33-
plt.title('subplot 2')
34-
plt.ylabel('Undamped')
35-
36-
plt.subplots_adjust(left=0.2, wspace=0.8, top=0.8)
23+
fig, axs = plt.subplots(2, 1, constrained_layout=True)
24+
axs[0].plot(t1, f(t1), 'o', t2, f(t2), '-')
25+
axs[0].set_title('subplot 1')
26+
axs[0].set_xlabel('distance (m)')
27+
axs[0].set_ylabel('Damped oscillation')
28+
fig.suptitle('This is a somewhat long figure title', fontsize=16)
29+
30+
axs[1].plot(t3, np.cos(2*np.pi*t3), '--')
31+
axs[1].set_xlabel('time (s)')
32+
axs[1].set_title('subplot 2')
33+
axs[1].set_ylabel('Undamped')
3734

3835
plt.show()

‎examples/text_labels_and_annotations/legend_demo.py

Copy file name to clipboardExpand all lines: examples/text_labels_and_annotations/legend_demo.py
+2-5Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
###############################################################################
6464
# Here we attach legends to more complex plots.
6565

66-
fig, axes = plt.subplots(3, 1)
66+
fig, axes = plt.subplots(3, 1, constrained_layout=True)
6767
top_ax, middle_ax, bottom_ax = axes
6868

6969
top_ax.bar([0, 1, 2], [0.2, 0.3, 0.1], width=0.4, label="Bar 1",
@@ -81,13 +81,12 @@
8181
bottom_ax.stem([0.3, 1.5, 2.7], [1, 3.6, 2.7], label="stem test")
8282
bottom_ax.legend()
8383

84-
plt.subplots_adjust(hspace=0.7)
8584
plt.show()
8685

8786
###############################################################################
8887
# Now we'll showcase legend entries with more than one legend key.
8988

90-
fig, (ax1, ax2) = plt.subplots(2, 1)
89+
fig, (ax1, ax2) = plt.subplots(2, 1, constrained_layout=True)
9190

9291
# First plot: two legend keys for a single entry
9392
p1 = ax1.scatter([1], [5], c='r', marker='s', s=100)
@@ -114,7 +113,6 @@
114113
l = ax2.legend([(rpos, rneg), (rneg, rpos)], ['pad!=0', 'pad=0'],
115114
handler_map={(rpos, rneg): HandlerTuple(ndivide=None),
116115
(rneg, rpos): HandlerTuple(ndivide=None, pad=0.)})
117-
118116
plt.show()
119117

120118
###############################################################################
@@ -172,7 +170,6 @@ def create_artists(self, legend, orig_handle,
172170
for i, color, style in zip(range(5), colors, styles):
173171
ax.plot(x, np.sin(x) - .1 * i, c=color, ls=style)
174172

175-
176173
# make proxy artists
177174
# make list of one line -- doesn't matter what the coordinates are
178175
line = [[(0, 0)]]

0 commit comments

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