8000 Use OO interface more in merged examples. · matplotlib/matplotlib@fd2540c · GitHub
[go: up one dir, main page]

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 fd2540c

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

File tree

9 files changed

+147
-147
lines changed

9 files changed

+147
-147
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/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/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

examples/statistics/boxplot_demo.py

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,34 @@
2525
flier_low = np.random.rand(10) * -100
2626
data = np.concatenate((spread, center, flier_high, flier_low), 0)
2727

28+
fig, axs = plt.subplots(2, 3)
29+
2830
# basic plot
29-
plt.boxplot(data)
31+
axs[0, 0].boxplot(data)
32+
axs[0, 0].set_title('basic plot')
3033

3134
# notched plot
32-
plt.figure()
33-
plt.boxplot(data, 1)
35+
axs[0, 1].boxplot(data, 1)
36+
axs[0, 1].set_title('notched plot')
3437

3538
# change outlier point symbols
36-
plt.figure()
37-
plt.boxplot(data, 0, 'gD')
39+
axs[0, 2].boxplot(data, 0, 'gD')
40+
axs[0, 2].set_title('change outlier\npoint symbols')
3841

3942
# don't show outlier points
40-
plt.figure()
41-
plt.boxplot(data, 0, '')
43+
axs[1, 0].boxplot(data, 0, '')
44+
axs[1, 0].set_title("don't show\noutlier points")
4245

4346
# horizontal boxes
44-
plt.figure()
45-
plt.boxplot(data, 0, 'rs', 0)
47+
axs[1, 1].boxplot(data, 0, 'rs', 0)
48+
axs[1, 1].set_title('horizontal boxes')
4649

4750
# change whisker length
48-
plt.figure()
49-
plt.boxplot(data, 0, 'rs', 0, 0.75)
51+
axs[1, 2].boxplot(data, 0, 'rs', 0, 0.75)
52+
axs[1, 2].set_title('change whisker length')
53+
54+
fig.subplots_adjust(left=0.08, right=0.98, bottom=0.05, top=0.9,
55+
hspace=0.4, wspace=0.3)
5056

5157
# fake up some more data
5258
spread = np.random.rand(50) * 100
@@ -56,15 +62,15 @@
5662
d2 = np.concatenate((spread, center, flier_high, flier_low), 0)
5763
data.shape = (-1, 1)
5864
d2.shape = (-1, 1)
59-
# data = concatenate( (data, d2), 1 )
6065
# Making a 2-D array only works if all the columns are the
6166
# same length. If they are not, then use a list instead.
6267
# This is actually more efficient because boxplot converts
6368
# a 2-D array into a list of vectors internally anyway.
6469
data = [data, d2, d2[::2, 0]]
65-
# multiple box plots on one figure
66-
plt.figure()
67-
plt.boxplot(data)
70+
71+
# Multiple box plots on one Axes
72+
fig, ax = plt.subplots()
73+
ax.boxplot(data)
6874

6975
plt.show()
7076

@@ -101,9 +107,9 @@
101107

102108
fig, ax1 = plt.subplots(figsize=(10, 6))
103109
fig.canvas.set_window_title('A Boxplot Example')
104-
plt.subplots_adjust(left=0.075, right=0.95, top=0.9, bottom=0.25)
110+
fig.subplots_adjust(left=0.075, right=0.95, top=0.9, bottom=0.25)
105111

106-
bp = plt.boxplot(data, notch=0, sym='+', vert=1, whis=1.5)
112+
bp = ax1.boxplot(data, notch=0, sym='+', vert=1, whis=1.5)
107113
plt.setp(bp['boxes'], color='black')
108114
plt.setp(bp['whiskers'], color='black')
109115
plt.setp(bp['fliers'], color='red', marker='+')
@@ -142,20 +148,20 @@
142148
for j in range(2):
143149
medianX.append(med.get_xdata()[j])
144150
medianY.append(med.get_ydata()[j])
145-
plt.plot(medianX, medianY, 'k')
151+
ax1.plot(medianX, medianY, 'k')
146152
medians[i] = medianY[0]
147153
# Finally, overplot the sample averages, with horizontal alignment
148154
# in the center of each box
149-
plt.plot([np.average(med.get_xdata())], [np.average(data[i])],
155+
ax1.plot([np.average(med.get_xdata())], [np.average(data[i])],
150156
color='w', marker='*', markeredgecolor='k')
151157

152158
# Set the axes ranges and axes labels
153159
ax1.set_xlim(0.5, numBoxes + 0.5)
154160
top = 40
155161
bottom = -5
156162
ax1.set_ylim(bottom, top)
157-
xtickNames = plt.setp(ax1, xticklabels=np.repeat(randomDists, 2))
158-
plt.setp(xtickNames, rotation=45, fontsize=8)
163+
ax1.set_xticklabels(np.repeat(randomDists, 2),
164+
rotation=45, fontsize=8)
159165

160166
# Due to the Y-axis scale being different across samples, it can be
161167
# hard to compare differences in medians across the samples. Add upper
@@ -171,16 +177,16 @@
171177
color=boxColors[k])
172178

173179
# Finally, add a basic legend
174-
plt.figtext(0.80, 0.08, str(N) + ' Random Numbers',
175-
backgroundcolor=boxColors[0], color='black', weight='roman',
176-
size='x-small')
177-
plt.figtext(0.80, 0.045, 'IID Bootstrap Resample',
178-
backgroundcolor=boxColors[1],
179-
color='white', weight='roman', size='x-small')
180-
plt.figtext(0.80, 0.015, '*', color='white', backgroundcolor='silver',
181-
weight='roman', size='medium')
182-
plt.figtext(0.815, 0.013, ' Average Value', color='black', weight='roman',
183-
size='x-small')
180+
fig.text(0.80, 0.08, str(N) + ' Random Numbers',
181+
backgroundcolor=boxColors[0], color='black', weight='roman',
182+
size='x-small')
183+
fig.text(0.80, 0.045, 'IID Bootstrap Resample',
184+
backgroundcolor=boxColors[1],
185+
color='white', weight='roman', size='x-small')
186+
fig.text(0.80, 0.015, '*', color='white', backgroundcolor='silver',
187+
weight='roman', size='medium')
188+
fig.text(0.815, 0.013, ' Average Value', color='black', weight='roman',
189+
size='x-small')
184190

185191
plt.show()
186192

0 commit comments

Comments
 (0)
0