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

Skip to content

Commit 3a41957

Browse files
committed
DOC: switch to OO interface in API examples
1 parent 2cdb5aa commit 3a41957

5 files changed

Lines changed: 37 additions & 32 deletions

File tree

examples/api/bbox_intersect.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
from matplotlib.transforms import Bbox
44
from matplotlib.path import Path
55

6-
rect = plt.Rectangle((-1, -1), 2, 2, facecolor="#aaaaaa")
7-
plt.gca().add_patch(rect)
8-
bbox = Bbox.from_bounds(-1, -1, 2, 2)
6+
left, bottom, width, height = (-1, -1, 2, 2)
7+
rect = plt.Rectangle((left, bottom), width, height, facecolor="#aaaaaa")
8+
9+
fig, ax = plt.subplots()
10+
ax.add_patch(rect)
11+
12+
bbox = Bbox.from_bounds(left, bottom, width, height)
913

1014
for i in range(12):
1115
vertices = (np.random.random((2, 2)) - 0.5) * 6.0
@@ -14,6 +18,6 @@
1418
color = 'r'
1519
else:
1620
color = 'b'
17-
plt.plot(vertices[:, 0], vertices[:, 1], color=color)
21+
ax.plot(vertices[:, 0], vertices[:, 1], color=color)
1822

1923
plt.show()

examples/api/legend_demo.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
d = c[::-1]
88

99
# Create plots with pre-defined labels.
10-
plt.plot(a, c, 'k--', label='Model length')
11-
plt.plot(a, d, 'k:', label='Data length')
12-
plt.plot(a, c + d, 'k', label='Total message length')
10+
fig, ax = plt.subplots()
11+
ax.plot(a, c, 'k--', label='Model length')
12+
ax.plot(a, d, 'k:', label='Data length')
13+
ax.plot(a, c + d, 'k', label='Total message length')
1314

14-
legend = plt.legend(loc='upper center', shadow=True, fontsize='x-large')
15+
legend = ax.legend(loc='upper center', shadow=True, fontsize='x-large')
1516

1617
# Put a nicer background color on the legend.
1718
legend.get_frame().set_facecolor('#00FFCC')

examples/api/patch_collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@
4242
p = PatchCollection(patches, alpha=0.4)
4343
p.set_array(np.array(colors))
4444
ax.add_collection(p)
45-
plt.colorbar(p)
45+
fig.colorbar(p, ax=ax)
4646

4747
plt.show()

examples/api/power_norm_demo.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@
33
import numpy as np
44
from numpy.random import multivariate_normal
55

6-
data = np.vstack([multivariate_normal([10, 10], [[3, 2], [2, 3]], size=100000),
7-
multivariate_normal([30, 20], [[2, 3], [1, 3]], size=1000)
8-
])
6+
data = np.vstack([
7+
multivariate_normal([10, 10], [[3, 2], [2, 3]], size=100000),
8+
multivariate_normal([30, 20], [[2, 3], [1, 3]], size=1000)
9+
])
910

1011
gammas = [0.8, 0.5, 0.3]
11-
xgrid = np.floor((len(gammas) + 1.) / 2)
12-
ygrid = np.ceil((len(gammas) + 1.) / 2)
1312

14-
plt.subplot(xgrid, ygrid, 1)
15-
plt.title('Linear normalization')
16-
plt.hist2d(data[:, 0], data[:, 1], bins=100)
13+
fig, axes = plt.subplots(nrows=2, ncols=2)
1714

18-
for i, gamma in enumerate(gammas):
19-
plt.subplot(xgrid, ygrid, i + 2)
20-
plt.title('Power law\n$(\gamma=%1.1f)$' % gamma)
21-
plt.hist2d(data[:, 0], data[:, 1],
22-
bins=100, norm=mcolors.PowerNorm(gamma))
15+
axes[0, 0].set_title('Linear normalization')
16+
axes[0, 0].hist2d(data[:, 0], data[:, 1], bins=100)
2317

24-
plt.tight_layout()
18+
for ax, gamma in zip(axes.flat[1:], gammas):
19+
ax.set_title('Power law $(\gamma=%1.1f)$' % gamma)
20+
ax.hist2d(data[:, 0], data[:, 1],
21+
bins=100, norm=mcolors.PowerNorm(gamma))
22+
23+
fig.tight_layout()
2524

2625
plt.show()

examples/api/radar_chart.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ def example_data():
169169
data = example_data()
170170
spoke_labels = data.pop(0)
171171

172-
fig = plt.figure(figsize=(9, 9))
172+
fig, axes = plt.subplots(figsize=(9, 9), nrows=2, ncols=2,
173+
subplot_kw=dict(projection='radar'))
173174
fig.subplots_adjust(wspace=0.25, hspace=0.20, top=0.85, bottom=0.05)
174175

175176
colors = ['b', 'r', 'g', 'm', 'y']
176177
# Plot the four cases from the example data on separate axes
177-
for n, (title, case_data) in enumerate(data):
178-
ax = fig.add_subplot(2, 2, n + 1, projection='radar')
179-
plt.rgrids([0.2, 0.4, 0.6, 0.8])
178+
for ax, (title, case_data) in zip(axes.flatten(), data):
179+
ax.set_rgrids([0.2, 0.4, 0.6, 0.8])
180180
ax.set_title(title, weight='bold', size='medium', position=(0.5, 1.1),
181181
horizontalalignment='center', verticalalignment='center')
182182
for d, color in zip(case_data, colors):
@@ -185,11 +185,12 @@ def example_data():
185185
ax.set_varlabels(spoke_labels)
186186

187187
# add legend relative to top-left plot
188-
plt.subplot(2, 2, 1)
188+
ax = axes[0, 0]
189189
labels = ('Factor 1', 'Factor 2', 'Factor 3', 'Factor 4', 'Factor 5')
190-
legend = plt.legend(labels, loc=(0.9, .95), labelspacing=0.1)
191-
plt.setp(legend.get_texts(), fontsize='small')
190+
legend = ax.legend(labels, loc=(0.9, .95), labelspacing=0.1, fontsize='small')
191+
192+
fig.text(0.5, 0.965, '5-Factor Solution Profiles Across Four Scenarios',
193+
horizontalalignment='center', color='black', weight='bold',
194+
size='large')
192195

193-
plt.figtext(0.5, 0.965, '5-Factor Solution Profiles Across Four Scenarios',
194-
ha='center', color='black', weight='bold', size='large')
195196
plt.show()

0 commit comments

Comments
 (0)