Skip to content

Commit 86d961c

Browse files
authored
Merge pull request #12379 from rth/use-np-full
MAINT Use np.full when possible
2 parents 2e9307e + fec1fa0 commit 86d961c

File tree

7 files changed

+15
-17
lines changed

7 files changed

+15
-17
lines changed

lib/matplotlib/axes/_axes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4000,7 +4000,7 @@ def dopatch(xs, ys, **kwargs):
40004000
# maybe draw the fliers
40014001
if showfliers:
40024002
# fliers coords
4003-
flier_x = np.ones(len(stats['fliers'])) * pos
4003+
flier_x = np.full(len(stats['fliers']), pos, dtype=np.float64)
40044004
flier_y = stats['fliers']
40054005

40064006
fliers.extend(doplot(

lib/matplotlib/hatch.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ def __init__(self, hatch, density):
169169
self.num_rows = (hatch.count('*')) * density
170170
path = Path.unit_regular_star(5)
171171
self.shape_vertices = path.vertices
172-
self.shape_codes = np.ones(len(self.shape_vertices)) * Path.LINETO
172+
self.shape_codes = np.full(len(self.shape_vertices), Path.LINETO,
173+
dtype=Path.code_type)
173174
self.shape_codes[0] = Path.MOVETO
174175
Shapes.__init__(self, hatch, density)
175176

lib/matplotlib/legend_handler.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def create_artists(self, legend, orig_handle,
231231
xdata, xdata_marker = self.get_xdata(legend, xdescent, ydescent,
232232
width, height, fontsize)
233233

234-
ydata = ((height - ydescent) / 2.) * np.ones(xdata.shape, float)
234+
ydata = np.full_like(xdata, ((height - ydescent) / 2))
235235
legline = Line2D(xdata, ydata)
236236

237237
self.update_prop(legline, orig_handle, legend)

lib/matplotlib/path.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def make_compound_path_from_polys(cls, XY):
312312
stride = numsides + 1
313313
nverts = numpolys * stride
314314
verts = np.zeros((nverts, 2))
315-
codes = np.ones(nverts, int) * cls.LINETO
315+
codes = np.full(nverts, cls.LINETO, dtype=cls.code_type)
316316
codes[0::stride] = cls.MOVETO
317317
codes[numsides::stride] = cls.CLOSEPOLY
318318
for i in range(numsides):
@@ -552,7 +552,8 @@ def interpolated(self, steps):
552552
vertices = simple_linear_interpolation(self.vertices, steps)
553553
codes = self.codes
554554
if codes is not None:
555-
new_codes = Path.LINETO * np.ones(((len(codes) - 1) * steps + 1, ))
555+
new_codes = np.full((len(codes) - 1) * steps + 1, Path.LINETO,
556+
dtype=self.code_type)
556557
new_codes[0::steps] = codes
557558
else:
558559
new_codes = None
@@ -802,7 +803,7 @@ def unit_circle_righthalf(cls):
802803

803804
float)
804805

805-
codes = cls.CURVE4 * np.ones(14)
806+
codes = np.full(14, cls.CURVE4, dtype=cls.code_type)
806807
codes[0] = cls.MOVETO
807808
codes[-1] = cls.CLOSEPOLY
808809

@@ -864,7 +865,7 @@ def arc(cls, theta1, theta2, n=None, is_wedge=False):
864865
if is_wedge:
865866
length = n * 3 + 4
866867
vertices = np.zeros((length, 2), float)
867-
codes = cls.CURVE4 * np.ones((length, ), cls.code_type)
868+
codes = np.full(length, cls.CURVE4, dtype=cls.code_type)
868869
vertices[1] = [xA[0], yA[0]]
869870
codes[0:2] = [cls.MOVETO, cls.LINETO]
870871
codes[-2:] = [cls.LINETO, cls.CLOSEPOLY]
@@ -873,7 +874,7 @@ def arc(cls, theta1, theta2, n=None, is_wedge=False):
873874
else:
874875
length = n * 3 + 1
875876
vertices = np.empty((length, 2), float)
876-
codes = cls.CURVE4 * np.ones((length, ), cls.code_type)
877+
codes = np.full(length, cls.CURVE4, dtype=cls.code_type)
877878
vertices[0] = [xA[0], yA[0]]
878879
codes[0] = cls.MOVETO
879880
vertex_offset = 1

lib/matplotlib/tri/triinterpolate.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ def get_Kff_and_Ff(self, J, ecc, triangles, Uc):
959959
"""
960960
ntri = np.size(ecc, 0)
961961
vec_range = np.arange(ntri, dtype=np.int32)
962-
c_indices = -np.ones(ntri, dtype=np.int32) # for unused dofs, -1
962+
c_indices = np.full(ntri, -1, dtype=np.int32) # for unused dofs, -1
963963
f_dof = [1, 2, 4, 5, 7, 8]
964964
c_dof = [0, 3, 6]
965965

lib/matplotlib/tri/trirefine.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def refine_triangulation(self, return_tri_index=False, subdiv=3):
111111
# We have to initialize found_index with -1 because some nodes
112112
# may very well belong to no triangle at all, e.g., in case of
113113
# Delaunay Triangulation with DuplicatePointWarning.
114-
found_index = - np.ones(refi_npts, dtype=np.int32)
114+
found_index = np.full(refi_npts, -1, dtype=np.int32)
115115
tri_mask = self._triangulation.mask
116116
if tri_mask is None:
117117
found_index[refi_triangles] = np.repeat(ancestors,
@@ -238,12 +238,8 @@ def _refine_triangulation_once(triangulation, ancestors=None):
238238
# (can be -1 if border edge)
239239
# For slave and master we will identify the apex pointing to the edge
240240
# start
241-
edge_elems = np.ravel(np.vstack([np.arange(ntri, dtype=np.int32),
242-
np.arange(ntri, dtype=np.int32),
243-
np.arange(ntri, dtype=np.int32)]))
244-
edge_apexes = np.ravel(np.vstack([np.zeros(ntri, dtype=np.int32),
245-
np.ones(ntri, dtype=np.int32),
246-
np.ones(ntri, dtype=np.int32)*2]))
241+
edge_elems = np.tile(np.arange(ntri, dtype=np.int32), 3)
242+
edge_apexes = np.repeat(np.arange(3, dtype=np.int32), ntri)
247243
edge_neighbors = neighbors[edge_elems, edge_apexes]
248244
mask_masters = (edge_elems > edge_neighbors)
249245

lib/matplotlib/tri/tritools.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def _total_to_compress_renum(mask, n=None):
292292
if n is None:
293293
n = np.size(mask)
294294
if mask is not None:
295-
renum = -np.ones(n, dtype=np.int32) # Default num is -1
295+
renum = np.full(n, -1, dtype=np.int32) # Default num is -1
296296
valid = np.arange(n, dtype=np.int32).compress(~mask, axis=0)
297297
renum[valid] = np.arange(np.size(valid, 0), dtype=np.int32)
298298
return renum

0 commit comments

Comments
 (0)