Skip to content

Commit 1504540

Browse files
committedDec 4, 2016
Use reshape instead of assigning to shape.
1 parent 46b9d47 commit 1504540

20 files changed

+63
-139
lines changed
 

‎examples/axes_grid1/simple_axesgrid.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
from mpl_toolkits.axes_grid1 import ImageGrid
33
import numpy as np
44

5-
im = np.arange(100)
6-
im.shape = 10, 10
5+
im = np.arange(100).reshape((10, 10))
76

87
fig = plt.figure(1, (4., 4.))
98
grid = ImageGrid(fig, 111, # similar to subplot(111)

‎examples/pylab_examples/agg_buffer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
l, b, w, h = agg.figure.bbox.bounds
2727
w, h = int(w), int(h)
2828

29-
X = np.fromstring(s, np.uint8)
30-
X.shape = h, w, 3
29+
X = np.fromstring(s, np.uint8).reshape((h, w, 3))
3130

3231
try:
3332
im = Image.fromstring("RGB", (w, h), s)

‎examples/pylab_examples/barcode_demo.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@
1414
fig = plt.figure()
1515

1616
# a vertical barcode -- this is broken at present
17-
x.shape = len(x), 1
1817
ax = fig.add_axes([0.1, 0.3, 0.1, 0.6], **axprops)
19-
ax.imshow(x, **barprops)
18+
ax.imshow(x.reshape((-1, 1)), **barprops)
2019

21-
x = x.copy()
2220
# a horizontal barcode
23-
x.shape = 1, len(x)
2421
ax = fig.add_axes([0.3, 0.1, 0.6, 0.1], **axprops)
25-
ax.imshow(x, **barprops)
22+
ax.imshow(x.reshape((1, -1)), **barprops)
2623

2724

2825
plt.show()

‎examples/pylab_examples/figimage_demo.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99

1010

1111
fig = plt.figure()
12-
Z = np.arange(10000.0)
13-
Z.shape = 100, 100
14-
Z[:, 50:] = 1.
12+
Z = np.arange(10000).reshape(100, 100)
13+
Z[:, 50:] = 1
1514

16-
im1 = plt.figimage(Z, xo=50, yo=0, origin='lower')
17-
im2 = plt.figimage(Z, xo=100, yo=100, alpha=.8, origin='lower')
15+
im1 = fig.figimage(Z, xo=50, yo=0, origin='lower')
16+
im2 = fig.figimage(Z, xo=100, yo=100, alpha=.8, origin='lower')
1817

1918
plt.show()

‎examples/pylab_examples/image_demo2.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77

88
datafile = cbook.get_sample_data('ct.raw.gz', asfileobj=True)
99
s = datafile.read()
10-
A = np.fromstring(s, np.uint16).astype(float)
11-
A *= 1.0 / max(A)
12-
A.shape = w, h
10+
A = np.fromstring(s, np.uint16).astype(float).reshape((w, h))
11+
A /= A.max()
1312

1413
extent = (0, 25, 0, 25)
1514
im = plt.imshow(A, cmap=plt.cm.hot, origin='upper', extent=extent)

‎examples/pylab_examples/image_origin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import matplotlib.pyplot as plt
88
import numpy as np
99

10-
x = np.arange(120)
11-
x.shape = (10, 12)
10+
x = np.arange(120).reshape((10, 12))
1211

1312
interp = 'bilinear'
1413
fig, axs = plt.subplots(nrows=2, sharex=True, figsize=(3, 5))

‎examples/pylab_examples/layer_images.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ def func3(x, y):
2828
extent = xmin, xmax, ymin, ymax
2929
fig = plt.figure(frameon=False)
3030

31-
Z1 = np.array(([0, 1]*4 + [1, 0]*4)*4)
32-
Z1.shape = (8, 8) # chessboard
31+
Z1 = np.add.outer(range(8), range(8)) % 2 # chessboard
3332
im1 = plt.imshow(Z1, cmap=plt.cm.gray, interpolation='nearest',
3433
extent=extent)
3534

‎examples/pylab_examples/mri_demo.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
# Data are 256x256 16 bit integers
1111
dfile = cbook.get_sample_data('s1045.ima.gz')
12-
im = np.fromstring(dfile.read(), np.uint16).astype(float)
13-
im.shape = (256, 256)
12+
im = np.fromstring(dfile.read(), np.uint16).reshape((256, 256))
1413
dfile.close()
1514

1615
ax.imshow(im, cmap=cm.gray)

‎examples/pylab_examples/mri_with_eeg.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
# Load the MRI data (256x256 16 bit integers)
1818
dfile = cbook.get_sample_data('s1045.ima.gz')
19-
im = np.fromstring(dfile.read(), np.uint16).astype(float)
20-
im.shape = (256, 256)
19+
im = np.fromstring(dfile.read(), np.uint16).astype(float).reshape((256, 256))
2120
dfile.close()
2221

2322
# Plot the MRI image

‎examples/user_interfaces/histogram_demo_canvasagg.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@
4848

4949
if 0:
5050
# convert to a numpy array
51-
X = numpy.fromstring(s, numpy.uint8)
52-
X.shape = h, w, 3
51+
X = numpy.fromstring(s, numpy.uint8).reshape((h, w, 3))
5352

5453
if 0:
5554
# pass off to PIL

‎lib/matplotlib/axes/_axes.py

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4264,35 +4264,23 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
42644264
d2 = (x - ix2 - 0.5) ** 2 + 3.0 * (y - iy2 - 0.5) ** 2
42654265
bdist = (d1 < d2)
42664266
if C is None:
4267-
accum = np.zeros(n)
4268-
# Create appropriate views into "accum" array.
4269-
lattice1 = accum[:nx1 * ny1]
4270-
lattice2 = accum[nx1 * ny1:]
4271-
lattice1.shape = (nx1, ny1)
4272-
lattice2.shape = (nx2, ny2)
4267+
lattice1 = np.zeros((nx1, ny1))
4268+
lattice2 = np.zeros((nx2, ny2))
42734269

42744270
for i in xrange(len(x)):
42754271
if bdist[i]:
4276-
if ((ix1[i] >= 0) and (ix1[i] < nx1) and
4277-
(iy1[i] >= 0) and (iy1[i] < ny1)):
4272+
if 0 <= ix1[i] < nx1 and 0 <= iy1[i] < ny1:
42784273
lattice1[ix1[i], iy1[i]] += 1
42794274
else:
4280-
if ((ix2[i] >= 0) and (ix2[i] < nx2) and
4281-
(iy2[i] >= 0) and (iy2[i] < ny2)):
4275+
if 0 <= ix2[i] < nx2 and 0 <= iy2[i] < ny2:
42824276
lattice2[ix2[i], iy2[i]] += 1
42834277

42844278
# threshold
42854279
if mincnt is not None:
4286-
for i in xrange(nx1):
4287-
for j in xrange(ny1):
4288-
if lattice1[i, j] < mincnt:
4289-
lattice1[i, j] = np.nan
4290-
for i in xrange(nx2):
4291-
for j in xrange(ny2):
4292-
if lattice2[i, j] < mincnt:
4293-
lattice2[i, j] = np.nan
4294-
accum = np.hstack((lattice1.astype(float).ravel(),
4295-
lattice2.astype(float).ravel()))
4280+
lattice1[lattice1 < mincnt] = np.nan
4281+
lattice2[lattice2 < mincnt] = np.nan
4282+
accum = np.hstack((lattice1.ravel(),
4283+
lattice2.ravel()))
42964284
good_idxs = ~np.isnan(accum)
42974285

42984286
else:
@@ -4311,12 +4299,10 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
43114299

43124300
for i in xrange(len(x)):
43134301
if bdist[i]:
4314-
if ((ix1[i] >= 0) and (ix1[i] < nx1) and
4315-
(iy1[i] >= 0) and (iy1[i] < ny1)):
4302+
if 0 <= ix1[i] < nx1 and 0 <= iy1[i] < ny1:
43164303
lattice1[ix1[i], iy1[i]].append(C[i])
43174304
else:
4318-
if ((ix2[i] >= 0) and (ix2[i] < nx2) and
4319-
(iy2[i] >= 0) and (iy2[i] < ny2)):
4305+
if 0 <= ix2[i] < nx2 and 0 <= iy2[i] < ny2:
43204306
lattice2[ix2[i], iy2[i]].append(C[i])
43214307

43224308
for i in xrange(nx1):
@@ -6612,7 +6598,6 @@ def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
66126598
pxx, freqs = mlab.psd(x=x, NFFT=NFFT, Fs=Fs, detrend=detrend,
66136599
window=window, noverlap=noverlap, pad_to=pad_to,
66146600
sides=sides, scale_by_freq=scale_by_freq)
6615-
pxx.shape = len(freqs),
66166601
freqs += Fc
66176602

66186603
if scale_by_freq in (None, True):
@@ -6736,7 +6721,6 @@ def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None,
67366721
pxy, freqs = mlab.csd(x=x, y=y, NFFT=NFFT, Fs=Fs, detrend=detrend,
67376722
window=window, noverlap=noverlap, pad_to=pad_to,
67386723
sides=sides, scale_by_freq=scale_by_freq)
6739-
pxy.shape = len(freqs),
67406724
# pxy is complex
67416725
freqs += Fc
67426726

‎lib/matplotlib/backends/backend_gdk.py

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ def draw_image(self, gc, x, y, im):
134134
int(x), int(y), cols, rows,
135135
gdk.RGB_DITHER_NONE, 0, 0)
136136

137-
138137
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
139138
x, y = int(x), int(y)
140139

@@ -158,54 +157,34 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
158157

159158
self.gdkDrawable.draw_layout(gc.gdkGC, x, y-h-b, layout)
160159

161-
162160
def _draw_mathtext(self, gc, x, y, s, prop, angle):
163161
ox, oy, width, height, descent, font_image, used_characters = \
164162
self.mathtext_parser.parse(s, self.dpi, prop)
165163

166-
if angle==90:
164+
if angle == 90:
167165
width, height = height, width
168166
x -= width
169167
y -= height
170168

171169
imw = font_image.get_width()
172170
imh = font_image.get_height()
173-
N = imw * imh
174-
175-
# a numpixels by num fonts array
176-
Xall = np.zeros((N,1), np.uint8)
177-
178-
image_str = font_image.as_str()
179-
Xall[:,0] = np.fromstring(image_str, np.uint8)
180-
181-
# get the max alpha at each pixel
182-
Xs = np.amax(Xall,axis=1)
183-
184-
# convert it to it's proper shape
185-
Xs.shape = imh, imw
186171

187172
pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=True,
188173
bits_per_sample=8, width=imw, height=imh)
189174

190175
array = pixbuf_get_pixels_array(pixbuf)
191176

192177
rgb = gc.get_rgb()
193-
array[:,:,0]=int(rgb[0]*255)
194-
array[:,:,1]=int(rgb[1]*255)
195-
array[:,:,2]=int(rgb[2]*255)
196-
array[:,:,3]=Xs
197-
198-
try: # new in 2.2
199-
# can use None instead of gc.gdkGC, if don't need clipping
200-
self.gdkDrawable.draw_pixbuf (gc.gdkGC, pixbuf, 0, 0,
201-
int(x), int(y), imw, imh,
202-
gdk.RGB_DITHER_NONE, 0, 0)
203-
except AttributeError:
204-
# deprecated in 2.2
205-
pixbuf.render_to_drawable(self.gdkDrawable, gc.gdkGC, 0, 0,
206-
int(x), int(y), imw, imh,
207-
gdk.RGB_DITHER_NONE, 0, 0)
178+
array[:,:,0] = int(rgb[0]*255)
179+
array[:,:,1] = int(rgb[1]*255)
180+
array[:,:,2] = int(rgb[2]*255)
181+
array[:,:,3] = (
182+
np.fromstring(font_image.as_str(), np.uint8).reshape((imh, imw)))
208183

184+
# can use None instead of gc.gdkGC, if don't need clipping
185+
self.gdkDrawable.draw_pixbuf(gc.gdkGC, pixbuf, 0, 0,
186+
int(x), int(y), imw, imh,
187+
gdk.RGB_DITHER_NONE, 0, 0)
209188

210189
def _draw_rotated_text(self, gc, x, y, s, prop, angle):
211190
"""
@@ -259,7 +238,6 @@ def _draw_rotated_text(self, gc, x, y, s, prop, angle):
259238
gdrawable.draw_image(ggc, imageVert, 0, 0, x, y, h, w)
260239
self.rotated[key] = imageVert
261240

262-
263241
def _get_pango_layout(self, s, prop):
264242
"""
265243
Create a pango layout instance for Text 's' with properties 'prop'.
@@ -293,7 +271,6 @@ def _get_pango_layout(self, s, prop):
293271
self.layoutd[key] = layout, inkRect, logicalRect
294272
return layout, inkRect, logicalRect
295273

296-
297274
def flipy(self):
298275
return True
299276

@@ -315,7 +292,6 @@ def get_text_width_height_descent(self, s, prop, ismath):
315292
def new_gc(self):
316293
return GraphicsContextGDK(renderer=self)
317294

318-
319295
def points_to_pixels(self, points):
320296
return points/72.0 * self.dpi
321297

‎lib/matplotlib/backends/backend_webagg_core.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ def get_diff_image(self):
207207
# The buffer is created as type uint32 so that entire
208208
# pixels can be compared in one numpy call, rather than
209209
# needing to compare each plane separately.
210-
buff = np.frombuffer(renderer.buffer_rgba(), dtype=np.uint32)
211-
buff.shape = (renderer.height, renderer.width)
210+
buff = (np.frombuffer(renderer.buffer_rgba(), dtype=np.uint32)
211+
.reshape((renderer.height, renderer.width)))
212212

213213
# If any pixels have transparency, we need to force a full
214214
# draw as we cannot overlay new on top of old.
@@ -219,10 +219,9 @@ def get_diff_image(self):
219219
output = buff
220220
else:
221221
self.set_image_mode('diff')
222-
last_buffer = np.frombuffer(self._last_renderer.buffer_rgba(),
223-
dtype=np.uint32)
224-
last_buffer.shape = (renderer.height, renderer.width)
225-
222+
last_buffer = (np.frombuffer(self._last_renderer.buffer_rgba(),
223+
dtype=np.uint32)
224+
.reshape((renderer.height, renderer.width)))
226225
diff = buff != last_buffer
227226
output = np.where(diff, buff, 0)
228227

‎lib/matplotlib/collections.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ class Collection(artist.Artist, cm.ScalarMappable):
8383
(i.e., a call to set_array has been made), at draw time a call to
8484
scalar mappable will be made to set the face colors.
8585
"""
86-
_offsets = np.array([], float)
87-
# _offsets must be a Nx2 array!
88-
_offsets.shape = (0, 2)
86+
_offsets = np.zeros((0, 2))
8987
_transOffset = transforms.IdentityTransform()
9088
#: Either a list of 3x3 arrays or an Nx3x3 array of transforms, suitable
9189
#: for the `all_transforms` argument to
@@ -147,8 +145,7 @@ def __init__(self,
147145
self._uniform_offsets = None
148146
self._offsets = np.array([[0, 0]], float)
149147
if offsets is not None:
150-
offsets = np.asanyarray(offsets)
151-
offsets.shape = (-1, 2) # Make it Nx2
148+
offsets = np.asanyarray(offsets).reshape((-1, 2))
152149
if transOffset is not None:
153150
self._offsets = offsets
154151
self._transOffset = transOffset
@@ -213,11 +210,10 @@ def get_datalim(self, transData):
213210
offsets = transOffset.transform_non_affine(offsets)
214211
transOffset = transOffset.get_affine()
215212

216-
offsets = np.asanyarray(offsets, float)
213+
offsets = np.asanyarray(offsets, float).reshape((-1, 2))
217214
if isinstance(offsets, np.ma.MaskedArray):
218215
offsets = offsets.filled(np.nan)
219216
# get_path_collection_extents handles nan but not masked arrays
220-
offsets.shape = (-1, 2) # Make it Nx2
221217

222218
if len(paths) and len(offsets):
223219
result = mpath.get_path_collection_extents(
@@ -255,8 +251,7 @@ def _prepare_points(self):
255251
ys = self.convert_yunits(offsets[:, 1])
256252
offsets = list(zip(xs, ys))
257253

258-
offsets = np.asanyarray(offsets, float)
259-
offsets.shape = (-1, 2) # Make it Nx2
254+
offsets = np.asanyarray(offsets, float).reshape((-1, 2))
260255

261256
if not transform.is_affine:
262257
paths = [transform.transform_path_non_affine(path)
@@ -436,8 +431,7 @@ def set_offsets(self, offsets):
436431
437432
ACCEPTS: float or sequence of floats
438433
"""
439-
offsets = np.asanyarray(offsets, float)
440-
offsets.shape = (-1, 2) # Make it Nx2
434+
offsets = np.asanyarray(offsets, float).reshape((-1, 2))
441435
#This decision is based on how they are initialized above
442436
if self._uniform_offsets is None:
443437
self._offsets = offsets
@@ -1889,8 +1883,7 @@ def draw(self, renderer):
18891883
ys = self.convert_yunits(self._offsets[:, 1])
18901884
offsets = list(zip(xs, ys))
18911885

1892-
offsets = np.asarray(offsets, float)
1893-
offsets.shape = (-1, 2) # Make it Nx2
1886+
offsets = np.asarray(offsets, float).reshape((-1, 2))
18941887

18951888
self.update_scalarmappable()
18961889

0 commit comments

Comments
 (0)
Please sign in to comment.