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

Skip to content

Commit 4c1a88f

Browse files
committed
Replace np.empty + ndarray.fill by np.full.
except for demo_agg_filter.py, which can be further simplified to use np.pad.
1 parent a4e3de2 commit 4c1a88f

File tree

6 files changed

+16
-29
lines changed

6 files changed

+16
-29
lines changed

examples/images_contours_and_fields/image_transparency_blend.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ def normal_pdf(x, mean, var):
4848
weights = weights_high + weights_low
4949

5050
# We'll also create a grey background into which the pixels will fade
51-
greys = np.empty(weights.shape + (3,), dtype=np.uint8)
52-
greys.fill(70)
51+
greys = np.full((*weights.shape, 3), 70, dtype=np.uint8)
5352

5453
# First we'll plot these blobs using only ``imshow``.
5554
vmax = np.abs(weights).max()

examples/misc/demo_agg_filter.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,13 @@ def __init__(self, pixels, color=None):
145145
self.color = color
146146

147147
def __call__(self, im, dpi):
148-
pad = self.pixels
149148
ny, nx, depth = im.shape
150-
new_im = np.empty([pad*2 + ny, pad*2 + nx, depth], dtype="d")
151-
alpha = new_im[:, :, 3]
152-
alpha.fill(0)
153-
alpha[pad:-pad, pad:-pad] = im[:, :, -1]
149+
alpha = np.pad(im[..., -1], self.pixels, "constant")
154150
alpha2 = np.clip(smooth2d(alpha, self.pixels/72.*dpi) * 5, 0, 1)
151+
new_im = np.empty((*alpha2.shape, 4))
155152
new_im[:, :, -1] = alpha2
156153
new_im[:, :, :-1] = self.color
157-
offsetx, offsety = -pad, -pad
158-
154+
offsetx, offsety = -self.pixels, -self.pixels
159155
return new_im, offsetx, offsety
160156

161157

lib/matplotlib/tests/test_mlab.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,9 @@ def test_stride_windows_n32_noverlap0_unflatten(self, axis):
127127

128128
def test_stride_ensure_integer_type(self):
129129
N = 100
130-
x = np.empty(N + 20, dtype='>f4')
131-
x.fill(np.NaN)
130+
x = np.full(N + 20, np.nan)
132131
y = x[10:-10]
133-
y.fill(0.3)
132+
y[:] = 0.3
134133
# previous to #3845 lead to corrupt access
135134
y_strided = mlab.stride_windows(y, n=33, noverlap=0.6)
136135
assert_array_equal(y_strided, 0.3)

lib/mpl_toolkits/axisartist/floating_axes.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ def transform_xy(x, y):
112112

113113
# find angles
114114
if self.nth_coord == 0:
115-
xx0 = np.empty_like(yy0)
116-
xx0.fill(self.value)
115+
xx0 = np.full_like(yy0, self.value)
117116

118117
xx1, yy1 = transform_xy(xx0, yy0)
119118

@@ -131,8 +130,7 @@ def transform_xy(x, y):
131130
labels = [l for l, m in zip(labels, mask) if m]
132131

133132
elif self.nth_coord == 1:
134-
yy0 = np.empty_like(xx0)
135-
yy0.fill(self.value)
133+
yy0 = np.full_like(xx0, self.value)
136134

137135
xx1, yy1 = transform_xy(xx0, yy0)
138136

@@ -372,15 +370,13 @@ def get_boundary(self):
372370
"""
373371
x0, x1, y0, y1 = self._extremes
374372
tr = self._aux_trans
375-
xx = np.linspace(x0, x1, 100)
376-
yy0, yy1 = np.empty_like(xx), np.empty_like(xx)
377-
yy0.fill(y0)
378-
yy1.fill(y1)
379373

374+
xx = np.linspace(x0, x1, 100)
375+
yy0 = np.full_like(xx, y0)
376+
yy1 = np.full_like(xx, y1)
380377
yy = np.linspace(y0, y1, 100)
381-
xx0, xx1 = np.empty_like(yy), np.empty_like(yy)
382-
xx0.fill(x0)
383-
xx1.fill(x1)
378+
xx0 = np.full_like(yy, x0)
379+
xx1 = np.full_like(yy, x1)
384380

385381
xxx = np.concatenate([xx[:-1], xx1[:-1], xx[-1:0:-1], xx0])
386382
yyy = np.concatenate([yy0[:-1], yy[:-1], yy1[:-1], yy[::-1]])

lib/mpl_toolkits/axisartist/grid_helper_curvelinear.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ def transform_xy(x, y):
231231

232232
# find angles
233233
if self.nth_coord == 0:
234-
xx0 = np.empty_like(yy0)
235-
xx0.fill(self.value)
234+
xx0 = np.full_like(yy0, self.value)
236235

237236
xx1, yy1 = transform_xy(xx0, yy0)
238237

@@ -248,8 +247,7 @@ def transform_xy(x, y):
248247
labels = [l for l, m in zip(labels, mask) if m]
249248

250249
elif self.nth_coord == 1:
251-
yy0 = np.empty_like(xx0)
252-
yy0.fill(self.value)
250+
yy0 = np.full_like(xx0, self.value)
253251

254252
xx1, yy1 = transform_xy(xx0, yy0)
255253

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,7 @@ def test_named_colors(self):
706706
x, y, z = np.indices((10, 10, 10))
707707
voxels = (x == y) | (y == z)
708708
voxels = voxels & ~(x * y * z < 1)
709-
colors = np.zeros((10, 10, 10), dtype=np.object_)
710-
colors.fill('C0')
709+
colors = np.full((10, 10, 10), 'C0', dtype=np.object_)
711710
colors[(x < 5) & (y < 5)] = '0.25'
712711
colors[(x + z) < 10] = 'cyan'
713712
ax.voxels(voxels, facecolors=colors)

0 commit comments

Comments
 (0)