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 cb89c38

Browse filesBrowse files
authored
Merge pull request #7067 from phobson/oo-interface-api-examples
DOC: OO interface in api and other examples
2 parents 1787fff + 32301f0 commit cb89c38
Copy full SHA for cb89c38
Expand file treeCollapse file tree

12 files changed

+85
-52
lines changed

‎examples/api/bbox_intersect.py

Copy file name to clipboardExpand all lines: examples/api/bbox_intersect.py
+8-4Lines 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/colorbar_basics.py

Copy file name to clipboard
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
# setup some generic data
5+
N = 37
6+
x, y = np.mgrid[:N, :N]
7+
Z = (np.cos(x*0.2) + np.sin(y*0.3))
8+
9+
# mask out the negative and positve values, respectively
10+
Zpos = np.ma.masked_less(Z, 0)
11+
Zneg = np.ma.masked_greater(Z, 0)
12+
13+
fig, (ax1, ax2) = plt.subplots(figsize=(8, 3), ncols=2)
14+
15+
# plot just the positive data and save the
16+
# color "mappable" object returned by ax1.imshow
17+
pos = ax1.imshow(Zpos, cmap='Blues', interpolation='none')
18+
19+
# add the colorbar using the figure's method,
20+
# telling which mappable we're talking about and
21+
# which axes object it should be near
22+
fig.colorbar(pos, ax=ax1)
23+
24+
# repeat everything above for the the negative data
25+
neg = ax2.imshow(Zneg, cmap='Reds_r', interpolation='none')
26+
fig.colorbar(neg, ax=ax2)
27+
28+
plt.show()

‎examples/api/legend_demo.py

Copy file name to clipboardExpand all lines: examples/api/legend_demo.py
+5-4Lines 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

Copy file name to clipboardExpand all lines: examples/api/patch_collection.py
+1-1Lines 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

Copy file name to clipboardExpand all lines: examples/api/power_norm_demo.py
+13-14Lines 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

Copy file name to clipboardExpand all lines: examples/api/radar_chart.py
+10-9Lines 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()

‎examples/color/color_cycle_demo.py

Copy file name to clipboardExpand all lines: examples/color/color_cycle_demo.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
ax1.set_title('Set axes color cycle to cmyk')
3232

3333
# Tweak spacing between subplots to prevent labels from overlapping
34-
plt.subplots_adjust(hspace=0.3)
34+
fig.subplots_adjust(hspace=0.3)
3535
plt.show()

‎examples/images_contours_and_fields/contourf_log.py

Copy file name to clipboardExpand all lines: examples/images_contours_and_fields/contourf_log.py
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030

3131
# Automatic selection of levels works; setting the
3232
# log locator tells contourf to use a log scale:
33-
cs = plt.contourf(X, Y, z, locator=ticker.LogLocator(), cmap=cm.PuBu_r)
33+
fig, ax = plt.subplots()
34+
cs = ax.contourf(X, Y, z, locator=ticker.LogLocator(), cmap=cm.PuBu_r)
3435

3536
# Alternatively, you can manually set the levels
3637
# and the norm:
@@ -41,6 +42,6 @@
4142

4243
# The 'extend' kwarg does not work yet with a log scale.
4344

44-
cbar = plt.colorbar()
45+
cbar = fig.colorbar(cs)
4546

4647
plt.show()

‎examples/images_contours_and_fields/image_demo.py

Copy file name to clipboardExpand all lines: examples/images_contours_and_fields/image_demo.py
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
image_file = cbook.get_sample_data('ada.png')
88
image = plt.imread(image_file)
99

10-
plt.imshow(image)
11-
plt.axis('off') # clear x- and y-axes
10+
fig, ax = plt.subplots()
11+
ax.imshow(image)
12+
ax.axis('off') # clear x- and y-axes
1213
plt.show()

‎examples/images_contours_and_fields/image_demo_clip_path.py

Copy file name to clipboardExpand all lines: examples/images_contours_and_fields/image_demo_clip_path.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
patch = patches.Circle((260, 200), radius=200, transform=ax.transData)
1515
im.set_clip_path(patch)
1616

17-
plt.axis('off')
17+
ax.axis('off')
1818
plt.show()

‎examples/images_contours_and_fields/interpolation_none_vs_nearest.py

Copy file name to clipboardExpand all lines: examples/images_contours_and_fields/interpolation_none_vs_nearest.py
+8-11Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@
2323
[0.6, 0.3, 0.0, 0.2], [0.7, 0.9, 0.4, 0.6]])
2424

2525
# Create a 2x2 table of plots
26-
fig = plt.figure(figsize=[8.0, 7.5])
27-
ax = plt.subplot(2, 2, 1)
28-
ax.imshow(big_im, interpolation='none')
29-
ax = plt.subplot(2, 2, 2)
30-
ax.imshow(big_im, interpolation='nearest')
31-
ax = plt.subplot(2, 2, 3)
32-
ax.imshow(small_im, interpolation='none')
33-
ax = plt.subplot(2, 2, 4)
34-
ax.imshow(small_im, interpolation='nearest')
35-
plt.subplots_adjust(left=0.24, wspace=0.2, hspace=0.1,
26+
fig, axes = plt.subplots(figsize=[8.0, 7.5], ncols=2, nrows=2)
27+
28+
axes[0, 0].imshow(big_im, interpolation='none')
29+
axes[0, 1].imshow(big_im, interpolation='nearest')
30+
axes[1, 0].imshow(small_im, interpolation='none')
31+
axes[1, 1].imshow(small_im, interpolation='nearest')
32+
fig.subplots_adjust(left=0.24, wspace=0.2, hspace=0.1,
3633
bottom=0.05, top=0.86)
3734

3835
# Label the rows and columns of the table
@@ -60,6 +57,6 @@
6057
pdf_im_path = cbook.get_sample_data('None_vs_nearest-pdf.png')
6158
pdf_im = plt.imread(pdf_im_path)
6259
fig2 = plt.figure(figsize=[8.0, 7.5])
63-
plt.figimage(pdf_im)
60+
fig2.figimage(pdf_im)
6461

6562
plt.show()

‎examples/images_contours_and_fields/streamplot_demo_masking.py

Copy file name to clipboardExpand all lines: examples/images_contours_and_fields/streamplot_demo_masking.py
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
U = np.ma.array(U, mask=mask)
1919
U[:20, :20] = np.nan
2020

21-
plt.streamplot(X, Y, U, V, color='r')
21+
fig, ax = plt.subplots()
22+
ax.streamplot(X, Y, U, V, color='r')
2223

23-
plt.imshow(~mask, extent=(-w, w, -w, w), alpha=0.5,
24-
interpolation='nearest', cmap=plt.cm.gray)
24+
ax.imshow(~mask, extent=(-w, w, -w, w), alpha=0.5,
25+
interpolation='nearest', cmap=plt.cm.gray)
2526

2627
plt.show()

0 commit comments

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