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 d52bdc3

Browse filesBrowse files
authored
Merge pull request #8677 from QuLogic/pylab-example-cleanups
DOC: Cleanup of merged pylab examples
2 parents e169aff + 869bd95 commit d52bdc3
Copy full SHA for d52bdc3
Expand file treeCollapse file tree

28 files changed

+243
-244
lines changed

‎examples/animation/image_slices_viewer.py

Copy file name to clipboardExpand all lines: examples/animation/image_slices_viewer.py
+5-4Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
66
"""
77
from __future__ import print_function
8-
import numpy
8+
9+
import numpy as np
910
import matplotlib.pyplot as plt
1011

1112

@@ -24,9 +25,9 @@ def __init__(self, ax, X):
2425
def onscroll(self, event):
2526
print("%s %s" % (event.button, event.step))
2627
if event.button == 'up':
27-
self.ind = numpy.clip(self.ind + 1, 0, self.slices - 1)
28+
self.ind = (self.ind + 1) % self.slices
2829
else:
29-
self.ind = numpy.clip(self.ind - 1, 0, self.slices - 1)
30+
self.ind = (self.ind - 1) % self.slices
3031
self.update()
3132

3233
def update(self):
@@ -37,7 +38,7 @@ def update(self):
3738

3839
fig, ax = plt.subplots(1, 1)
3940

40-
X = numpy.random.rand(20, 20, 40)
41+
X = np.random.rand(20, 20, 40)
4142

4243
tracker = IndexTracker(ax, X)
4344

‎examples/event_handling/pick_event_demo2.py

Copy file name to clipboardExpand all lines: examples/event_handling/pick_event_demo2.py
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
mean vs stddev. When you click on one of the mu, sigma points, plot the raw
88
data from the dataset that generated the mean and stddev.
99
"""
10-
import numpy
10+
import numpy as np
1111
import matplotlib.pyplot as plt
1212

1313

14-
X = numpy.random.rand(100, 1000)
15-
xs = numpy.mean(X, axis=1)
16-
ys = numpy.std(X, axis=1)
14+
X = np.random.rand(100, 1000)
15+
xs = np.mean(X, axis=1)
16+
ys = np.std(X, axis=1)
1717

1818
fig, ax = plt.subplots()
1919
ax.set_title('click on point to plot time series')

‎examples/event_handling/zoom_window.py

Copy file name to clipboardExpand all lines: examples/event_handling/zoom_window.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
points**2, so their size is independent of the zoom
1515
"""
1616
from matplotlib.pyplot import figure, show
17-
import numpy
17+
import numpy as np
1818
figsrc = figure()
1919
figzoom = figure()
2020

@@ -23,7 +23,7 @@
2323
autoscale_on=False)
2424
axsrc.set_title('Click to zoom')
2525
axzoom.set_title('zoom window')
26-
x, y, s, c = numpy.random.rand(4, 200)
26+
x, y, s, c = np.random.rand(4, 200)
2727
s *= 200
2828

2929

‎examples/frontpage/membrane.py

Copy file name to clipboardExpand all lines: examples/frontpage/membrane.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import numpy as np
1212

1313

14-
datafile = cbook.get_sample_data('membrane.dat', asfileobj=False)
15-
x = np.fromfile(datafile, np.float32)
14+
with cbook.get_sample_data('membrane.dat') as datafile:
15+
x = np.fromfile(datafile, np.float32)
1616
# 0.0005 is the sample interval
1717

1818
fig, ax = plt.subplots()

‎examples/images_contours_and_fields/image_clip_path.py

Copy file name to clipboardExpand all lines: examples/images_contours_and_fields/image_clip_path.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import matplotlib.cbook as cbook
1111

1212

13-
image_file = cbook.get_sample_data('grace_hopper.png')
14-
image = plt.imread(image_file)
13+
with cbook.get_sample_data('grace_hopper.png') as image_file:
14+
image = plt.imread(image_file)
1515

1616
fig, ax = plt.subplots()
1717
im = ax.imshow(image)

‎examples/images_contours_and_fields/image_demo.py

Copy file name to clipboardExpand all lines: examples/images_contours_and_fields/image_demo.py
+22-24Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
# It is also possible to show images of pictures.
4242

4343
# A sample image
44-
image_file = cbook.get_sample_data('ada.png')
45-
image = plt.imread(image_file)
44+
with cbook.get_sample_data('ada.png') as image_file:
45+
image = plt.imread(image_file)
4646

4747
fig, ax = plt.subplots()
4848
ax.imshow(image)
@@ -53,19 +53,20 @@
5353

5454
w, h = 512, 512
5555

56-
datafile = cbook.get_sample_data('ct.raw.gz', asfileobj=True)
57-
s = datafile.read()
56+
with cbook.get_sample_data('ct.raw.gz', asfileobj=True) as datafile:
57+
s = datafile.read()
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,26 +122,21 @@
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

138134

139135
###############################################################################
140136
# You can specify whether images should be plotted with the array origin
141-
# x[0,0] in the upper left or upper right by using the origin parameter.
142-
# You can also control the default be setting image.origin in your
143-
# matplotlibrc file; see http://matplotlib.org/matplotlibrc
137+
# x[0,0] in the upper left or lower right by using the origin parameter.
138+
# You can also control the default setting image.origin in your
139+
# :ref:`matplotlibrc file <customizing-with-matplotlibrc-files>`
144140

145141
x = np.arange(120).reshape((10, 12))
146142

@@ -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/ellipse_demo.py

Copy file name to clipboardExpand all lines: examples/pylab_examples/ellipse_demo.py
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55
66
"""
77
import matplotlib.pyplot as plt
8-
import numpy.random as rnd
8+
import numpy as np
99
from matplotlib.patches import Ellipse
1010

1111
NUM = 250
1212

13-
ells = [Ellipse(xy=rnd.rand(2)*10, width=rnd.rand(), height=rnd.rand(), angle=rnd.rand()*360)
13+
ells = [Ellipse(xy=np.random.rand(2)*10,
14+
width=np.random.rand(), height=np.random.rand(),
15+
angle=np.random.rand()*360)
1416
for i in range(NUM)]
1517

1618
fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
1719
for e in ells:
1820
ax.add_artist(e)
1921
e.set_clip_box(ax.bbox)
20-
e.set_alpha(rnd.rand())
21-
e.set_facecolor(rnd.rand(3))
22+
e.set_alpha(np.random.rand())
23+
e.set_facecolor(np.random.rand(3))
2224

2325
ax.set_xlim(0, 10)
2426
ax.set_ylim(0, 10)

‎examples/pylab_examples/fancybox_demo.py

Copy file name to clipboardExpand all lines: examples/pylab_examples/fancybox_demo.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test2(ax):
9393
#p_fancy.set_boxstyle("round", pad=0.1, rounding_size=0.2)
9494

9595
ax.text(0.1, 0.8,
96-
' boxstyle="round,pad=0.1\n rounding\\_size=0.2"',
96+
' boxstyle="round,pad=0.1\n rounding_size=0.2"',
9797
size=10, transform=ax.transAxes)
9898

9999
# draws control points for the fancy box.
@@ -118,7 +118,7 @@ def test3(ax):
118118
ax.add_patch(p_fancy)
119119

120120
ax.text(0.1, 0.8,
121-
' boxstyle="round,pad=0.1"\n mutation\\_scale=2',
121+
' boxstyle="round,pad=0.1"\n mutation_scale=2',
122122
size=10, transform=ax.transAxes)
123123

124124
# draws control points for the fancy box.
@@ -152,7 +152,7 @@ def test4(ax):
152152
ax.add_patch(p_fancy)
153153

154154
ax.text(0.1, 0.8,
155-
' boxstyle="round,pad=0.3"\n mutation\\_aspect=.5',
155+
' boxstyle="round,pad=0.3"\n mutation_aspect=.5',
156156
size=10, transform=ax.transAxes)
157157

158158
draw_bbox(ax, bb)

‎examples/pylab_examples/line_collection.py

Copy file name to clipboardExpand all lines: examples/pylab_examples/line_collection.py
+2-3Lines 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

Copy file name to clipboardExpand all lines: examples/pylab_examples/major_minor_demo.py
+5-5Lines 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/mri_demo.py

Copy file name to clipboardExpand all lines: examples/pylab_examples/mri_demo.py
+3-4Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
import matplotlib.cm as cm
1313
import numpy as np
1414

15-
fig, ax = plt.subplots(num="MRI_demo")
1615

1716
# Data are 256x256 16 bit integers
18-
dfile = cbook.get_sample_data('s1045.ima.gz')
19-
im = np.fromstring(dfile.read(), np.uint16).reshape((256, 256))
20-
dfile.close()
17+
with cbook.get_sample_data('s1045.ima.gz') as dfile:
18+
im = np.fromstring(dfile.read(), np.uint16).reshape((256, 256))
2119

20+
fig, ax = plt.subplots(num="MRI_demo")
2221
ax.imshow(im, cmap=cm.gray)
2322
ax.axis('off')
2423

‎examples/pylab_examples/mri_with_eeg.py

Copy file name to clipboardExpand all lines: examples/pylab_examples/mri_with_eeg.py
+4-6Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
fig = plt.figure("MRI_with_EEG")
2121

2222
# Load the MRI data (256x256 16 bit integers)
23-
dfile = cbook.get_sample_data('s1045.ima.gz')
24-
im = np.fromstring(dfile.read(), np.uint16).reshape((256, 256))
25-
dfile.close()
23+
with cbook.get_sample_data('s1045.ima.gz') as dfile:
24+
im = np.fromstring(dfile.read(), np.uint16).reshape((256, 256))
2625

2726
# Plot the MRI image
2827
ax0 = fig.add_subplot(2, 2, 1)
@@ -43,9 +42,8 @@
4342

4443
# Load the EEG data
4544
numSamples, numRows = 800, 4
46-
eegfile = cbook.get_sample_data('eeg.dat', asfileobj=False)
47-
print('Loading EEG %s' % eegfile)
48-
data = np.fromfile(eegfile, dtype=float)
45+
with cbook.get_sample_data('eeg.dat') as eegfile:
46+
data = np.fromfile(eegfile, dtype=float)
4947
data.shape = (numSamples, numRows)
5048
t = 10.0 * np.arange(numSamples) / numSamples
5149

0 commit comments

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