8000 convert some sample plots to use plt.subplots() instead of other methods · matplotlib/matplotlib@a2765d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit a2765d5

Browse files
committed
convert some sample plots to use plt.subplots() instead of other methods
1 parent cb34934 commit a2765d5

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

examples/images_contours_and_fields/plot_streamplot.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,30 @@
2222
V = 1 + X - Y**2
2323
speed = np.sqrt(U**2 + V**2)
2424

25-
fig = plt.figure(figsize=(7, 9))
26-
gs = gridspec.GridSpec(nrows=3, ncols=2, height_ratios=[1, 1, 2])
25+
26+
gs = {'height_ratios': [1, 1, 2]}
27+
fig, ((ax0, ax1), (ax2, ax3), (ax4, ax5)) = plt.subplots(nrows=3, ncols=2, gridspec_kw=gs, figsize=(7, 9))
28+
2729

2830
# Varying density along a streamline
29-
ax0 = fig.add_subplot(gs[0, 0])
3031
ax0.streamplot(X, Y, U, V, density=[0.5, 1])
3132
ax0.set_title('Varying Density')
3233

3334
# Varying color along a streamline
34-
ax1 = fig.add_subplot(gs[0, 1])
35+
plt.sca(ax1)
3536
strm = ax1.streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn')
3637
fig.colorbar(strm.lines)
3738
ax1.set_title('Varying Color')
3839

3940
# Varying line width along a streamline
40-
ax2 = fig.add_subplot(gs[1, 0])
4141
lw = 5*speed / speed.max()
4242
ax2.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)
4343
ax2.set_title('Varying Line Width')
4444

4545
# Controlling the starting points of the streamlines
4646
seed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1, 0, 1, 2, 2]])
4747

48-
ax3 = fig.add_subplot(gs[1, 1])
48+
plt.sca(ax3)
4949
strm = ax3.streamplot(X, Y, U, V, color=U, linewidth=2,
5050
cmap='autumn', start_points=seed_points.T)
5151
fig.colorbar(strm.lines)
@@ -61,6 +61,9 @@
6161
U[:20, :20] = np.nan
6262
U = np.ma.array(U, mask=mask)
6363

64+
gs = gridspec.GridSpec(nrows=3, ncols=2, height_ratios=[1, 1, 2])
65+
ax4.remove()
66+
ax5.remove()
6467
ax4 = fig.add_subplot(gs[2:, :])
6568
ax4.streamplot(X, Y, U, V, color='r')
6669
ax4.set_title('Streamplot with Masking')

examples/pie_and_polar_charts/polar_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
r = np.arange(0, 2, 0.01)
1313
theta = 2 * np.pi * r
1414

15-
ax = plt.subplot(111, projection='polar')
15+
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
1616
ax.plot(theta, r)
1717
ax.set_rmax(2)
1818
ax.set_rticks([0.5, 1, 1.5, 2]) # Less radial ticks

examples/showcase/xkcd.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
# Based on "Stove Ownership" from XKCD by Randall Munroe
1515
# https://xkcd.com/418/
1616

17-
fig = plt.figure()
18-
ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))
17+
fig, ax = plt.subplots()
18+
ax.set_position((0.1, 0.2, 0.8, 0.7))
1919
ax.spines['right'].set_color('none')
2020
ax.spines['top'].set_color('none')
2121
ax.set_xticks([])
@@ -44,8 +44,8 @@
4444
# Based on "The Data So Far" from XKCD by Randall Munroe
4545
# https://xkcd.com/373/
4646

47-
fig = plt.figure()
48-
ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))
47+
fig, ax = plt.subplots()
48+
ax.set_position((0.1, 0.2, 0.8, 0.7))
4949
ax.bar([0, 1], [0, 100], 0.25)
5050
ax.spines['right'].set_color('none')
5151
ax.spines['top'].set_color('none')

examples/subplots_axes_and_figures/subplot.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
1616
y2 = np.cos(2 * np.pi * x2)
1717

18-
plt.subplot(2, 1, 1)
19-
plt.plot(x1, y1, 'o-')
20-
plt.title('A tale of 2 subplots')
21-
plt.ylabel('Damped oscillation')
22-
23-
plt.subplot(2, 1, 2)
24-
plt.plot(x2, y2, '.-')
25-
plt.xlabel('time (s)')
26-
plt.ylabel('Undamped')
18+
fig, (ax1, ax2) = plt.subplots(2, 1)
19+
fig.suptitle('A tale of 2 subplots')
20+
21+
ax1.plot(x1, y1, 'o-')
22+
ax1.set_ylabel('Damped oscillation')
23+
24+
ax2.plot(x2, y2, '.-')
25+
ax2.set_xlabel('time (s)')
26+
ax2.set_ylabel('Undamped')
2727

2828
plt.show()

0 commit comments

Comments
 (0)
0