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

Skip to content

Commit dc0f0c1

Browse files
committed
Use OO interface more in merged examples.
1 parent e7084ad commit dc0f0c1

File tree

11 files changed

+164
-164
lines changed

11 files changed

+164
-164
lines changed

examples/images_contours_and_fields/image_demo.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@
5858
A = np.fromstring(s, np.uint16).astype(float).reshape((w, h))
5959
A /= A.max()
6060

61+
fig, ax = plt.subplots()
6162
extent = (0, 25, 0, 25)
62-
im = plt.imshow(A, cmap=plt.cm.hot, origin='upper', extent=extent)
63+
im = ax.imshow(A, cmap=plt.cm.hot, origin='upper', extent=extent)
6364

6465
markers = [(15.9, 14.5), (16.8, 15)]
6566
x, y = zip(*markers)
66-
plt.plot(x, y, 'o')
67+
ax.plot(x, y, 'o')
6768

68-
plt.title('CT density')
69+
ax.set_title('CT density')
6970

7071
plt.show()
7172

@@ -121,17 +122,12 @@
121122
# suggested.
122123

123124
A = np.random.rand(5, 5)
124-
plt.figure(1)
125-
plt.imshow(A, interpolation='nearest')
126-
plt.grid(True)
127-
128-
plt.figure(2)
129-
plt.imshow(A, interpolation='bilinear')
130-
plt.grid(True)
131125

132-
plt.figure(3)
133-
plt.imshow(A, interpolation='bicubic')
134-
plt.grid(True)
126+
fig, axs = plt.subplots(1, 3, figsize=(10, 3))
127+
for ax, interp in zip(axs, ['nearest', 'bilinear', 'bicubic']):
128+
ax.imshow(A, interpolation=interp)
129+
ax.set_title(interp.capitalize())
130+
ax.grid(True)
135131

136132
plt.show()
137133

@@ -166,11 +162,13 @@
166162

167163
path = Path([[0, 1], [1, 0], [0, -1], [-1, 0], [0, 1]])
168164
patch = PathPatch(path, facecolor='none')
169-
plt.gca().add_patch(patch)
170165

171-
im = plt.imshow(Z, interpolation='bilinear', cmap=cm.gray,
172-
origin='lower', extent=[-3, 3, -3, 3],
173-
clip_path=patch, clip_on=True)
166+
fig, ax = plt.subplots()
167+
ax.add_patch(patch)
168+
169+
im = ax.imshow(Z, interpolation='bilinear', cmap=cm.gray,
170+
origin='lower', extent=[-3, 3, -3, 3],
171+
clip_path=patch, clip_on=True)
174172
im.set_clip_path(patch)
175173

176174
plt.show()

examples/pylab_examples/line_collection.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
segs = np.ma.masked_where((segs > 50) & (segs < 60), segs)
3131

3232
# We need to set the plot limits.
33-
ax = plt.axes()
33+
fig, ax = plt.subplots()
3434
ax.set_xlim(x.min(), x.max())
3535
ax.set_ylim(ys.min(), ys.max())
3636

@@ -60,7 +60,7 @@
6060
ys = [x + i for i in x]
6161

6262
# We need to set the plot limits, they will not autoscale
63-
ax = plt.axes()
63+
fig, ax = plt.subplots()
6464
ax.set_xlim(np.min(x), np.max(x))
6565
ax.set_ylim(np.min(ys), np.max(ys))
6666

@@ -77,7 +77,6 @@
7777
linestyles='solid')
7878
line_segments.set_array(x)
7979
ax.add_collection(line_segments)
80-
fig = plt.gcf()
8180
axcb = fig.colorbar(line_segments)
8281
axcb.set_label('Line Number')
8382
ax.set_title('Line Collection with mapped colors')

examples/pylab_examples/major_minor_demo.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
s = np.sin(0.1*np.pi*t)*np.exp(-t*0.01)
4848

4949
fig, ax = plt.subplots()
50-
plt.plot(t, s)
50+
ax.plot(t, s)
5151

5252
ax.xaxis.set_major_locator(majorLocator)
5353
ax.xaxis.set_major_formatter(majorFormatter)
@@ -76,12 +76,12 @@
7676
s = np.sin(2*np.pi*t)*np.exp(-t*0.01)
7777

7878
fig, ax = plt.subplots()
79-
plt.plot(t, s)
79+
ax.plot(t, s)
8080

8181
ax.xaxis.set_minor_locator(minorLocator)
8282

83-
plt.tick_params(which='both', width=2)
84-
plt.tick_params(which='major', length=7)
85-
plt.tick_params(which='minor', length=4, color='r')
83+
ax.tick_params(which='both', width=2)
84+
ax.tick_params(which='major', length=7)
85+
ax.tick_params(which='minor', length=4, color='r')
8686

8787
plt.show()

examples/pylab_examples/pcolor_demo.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121
Z = np.random.rand(6, 10)
2222

23-
plt.subplot(2, 1, 1)
24-
c = plt.pcolor(Z)
25-
plt.title('default: no edges')
23+
fig, (ax0, ax1) = plt.subplots(2, 1)
2624

27-
plt.subplot(2, 1, 2)
28-
c = plt.pcolor(Z, edgecolors='k', linewidths=4)
29-
plt.title('thick edges')
25+
c = ax0.pcolor(Z)
26+
ax0.set_title('default: no edges')
27+
28+
c = ax1.pcolor(Z, edgecolors='k', linewidths=4)
29+
ax1.set_title('thick edges')
3030

3131
plt.show()
3232

@@ -49,37 +49,35 @@
4949
z = z[:-1, :-1]
5050
z_min, z_max = -np.abs(z).max(), np.abs(z).max()
5151

52+
fig, axs = plt.subplots(2, 2)
5253

53-
plt.subplot(2, 2, 1)
54-
plt.pcolor(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
55-
plt.title('pcolor')
54+
ax = axs[0, 0]
55+
c = ax.pcolor(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
56+
ax.set_title('pcolor')
5657
# set the limits of the plot to the limits of the data
57-
plt.axis([x.min(), x.max(), y.min(), y.max()])
58-
plt.colorbar()
59-
58+
ax.axis([x.min(), x.max(), y.min(), y.max()])
59+
fig.colorbar(c, ax=ax)
6060

61-
plt.subplot(2, 2, 2)
62-
plt.pcolormesh(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
63-
plt.title('pcolormesh')
61+
ax = axs[0, 1]
62+
c = ax.pcolormesh(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
63+
ax.set_title('pcolormesh')
6464
# set the limits of the plot to the limits of the data
65-
plt.axis([x.min(), x.max(), y.min(), y.max()])
66-
plt.colorbar()
67-
65+
ax.axis([x.min(), x.max(), y.min(), y.max()])
66+
fig.colorbar(c, ax=ax)
6867

69-
plt.subplot(2, 2, 3)
70-
plt.imshow(z, cmap='RdBu', vmin=z_min, vmax=z_max,
71-
extent=[x.min(), x.max(), y.min(), y.max()],
72-
interpolation='nearest', origin='lower')
73-
plt.title('image (nearest)')
74-
plt.colorbar()
68+
ax = axs[1, 0]
69+
c = ax.imshow(z, cmap='RdBu', vmin=z_min, vmax=z_max,
70+
extent=[x.min(), x.max(), y.min(), y.max()],
71+
interpolation='nearest', origin='lower')
72+
ax.set_title('image (nearest)')
73+
fig.colorbar(c, ax=ax)
7574

75+
ax = axs[1, 1]
76+
c = ax.pcolorfast(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
77+
ax.set_title('pcolorfast')
78+
fig.colorbar(c, ax=ax)
7679

77-
ax = plt.subplot(2, 2, 4)
78-
ax.pcolorfast(x, y, z, cmap='RdBu', vmin=z_min, vmax=z_max)
79-
plt.title('pcolorfast')
80-
plt.colorbar()
81-
82-
plt.subplots_adjust(wspace=0.5, hspace=0.5)
80+
fig.subplots_adjust(wspace=0.5, hspace=0.5)
8381

8482
plt.show()
8583

@@ -98,12 +96,13 @@
9896
# linear scale only shows the spike.
9997
Z1 = bivariate_normal(X, Y, 0.1, 0.2, 1.0, 1.0) + 0.1 * bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
10098

101-
plt.subplot(2, 1, 1)
102-
plt.pcolor(X, Y, Z1, norm=LogNorm(vmin=Z1.min(), vmax=Z1.max()), cmap='PuBu_r')
103-
plt.colorbar()
99+
fig, (ax0, ax1) = plt.subplots(2, 1)
100+
101+
c = ax0.pcolor(X, Y, Z1,
102+
norm=LogNorm(vmin=Z1.min(), vmax=Z1.max()), cmap='PuBu_r')
103+
fig.colorbar(c, ax=ax0)
104104

105-
plt.subplot(2, 1, 2)
106-
plt.pcolor(X, Y, Z1, cmap='PuBu_r')
107-
plt.colorbar()
105+
c = ax1.pcolor(X, Y, Z1, cmap='PuBu_r')
106+
fig.colorbar(c, ax=ax1)
108107

109108
plt.show()

examples/pylab_examples/spy_demos.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
Plot the sparsity pattern of arrays
77
"""
88

9-
from matplotlib.pyplot import figure, show
9+
import matplotlib.pyplot as plt
1010
import numpy as np
1111

12-
fig = figure()
13-
ax1 = fig.add_subplot(221)
14-
ax2 = fig.add_subplot(222)
15-
ax3 = fig.add_subplot(223)
16-
ax4 = fig.add_subplot(224)
12+
fig, axs = plt.subplots(2, 2)
13+
ax1 = axs[0, 0]
14+
ax2 = axs[0, 1]
15+
ax3 = axs[1, 0]
16+
ax4 = axs[1, 1]
1717

1818
x = np.random.randn(20, 20)
19-
x[5] = 0.
19+
x[5, :] = 0.
2020
x[:, 12] = 0.
2121

2222
ax1.spy(x, markersize=5)
@@ -25,4 +25,4 @@
2525
ax3.spy(x)
2626
ax4.spy(x, precision=0.1)
2727

28-
show()
28+
plt.show()

examples/pylab_examples/stackplot_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ def bump(a):
5555

5656
d = layers(3, 100)
5757

58-
plt.subplots()
59-
plt.stackplot(range(100), d.T, baseline='wiggle')
58+
fig, ax = plt.subplots()
59+
ax.stackplot(range(100), d.T, baseline='wiggle')
6060
plt.show()

examples/pylab_examples/subplot_toolbar.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
import matplotlib.pyplot as plt
88
import numpy as np
99

10-
fig = plt.figure()
11-
plt.subplot(221)
12-
plt.imshow(np.random.random((100, 100)))
13-
plt.subplot(222)
14-
plt.imshow(np.random.random((100, 100)))
15-
plt.subplot(223)
16-
plt.imshow(np.random.random((100, 100)))
17-
plt.subplot(224)
18-
plt.imshow(np.random.random((100, 100)))
10+
fig, axs = plt.subplots(2, 2)
11+
12+
axs[0, 0].imshow(np.random.random((100, 100)))
13+
14+
axs[0, 1].imshow(np.random.random((100, 100)))
15+
16+
axs[1, 0].imshow(np.random.random((100, 100)))
17+
18+
axs[1, 1].imshow(np.random.random((100, 100)))
1919

2020
plt.subplot_tool()
2121
plt.show()

examples/statistics/barchart_demo.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,24 @@
3434
opacity = 0.4
3535
error_config = {'ecolor': '0.3'}
3636

37-
rects1 = plt.bar(index, means_men, bar_width,
38-
alpha=opacity,
39-
color='b',
40-
yerr=std_men,
41-
error_kw=error_config,
42-
label='Men')
43-
44-
rects2 = plt.bar(index + bar_width, means_women, bar_width,
45-
alpha=opacity,
46-
color='r',
47-
yerr=std_women,
48-
error_kw=error_config,
49-
label='Women')
50-
51-
plt.xlabel('Group')
52-
plt.ylabel('Scores')
53-
plt.title('Scores by group and gender')
54-
plt.xticks(index + bar_width / 2, ('A', 'B', 'C', 'D', 'E'))
55-
plt.legend()
56-
57-
plt.tight_layout()
37+
rects1 = ax.bar(index, means_men, bar_width,
38+
alpha=opacity, color='b',
39+
yerr=std_men, error_kw=error_config,
40+
label='Men')
41+
42+
rects2 = ax.bar(index + bar_width, means_women, bar_width,
43+
alpha=opacity, color='r',
44+
yerr=std_women, error_kw=error_config,
45+
label='Women')
46+
47+
ax.set_xlabel('Group')
48+
ax.set_ylabel('Scores')
49+
ax.set_title('Scores by group and gender')
50+
ax.set_xticks(index + bar_width / 2)
51+
ax.set_xticklabels(('A', 'B', 'C', 'D', 'E'))
52+
ax.legend()
53+
54+
fig.tight_layout()
5855
plt.show()
5956

6057

0 commit comments

Comments
 (0)