Skip to content

Commit 9de15a0

Browse files
committed
Avoid memory copies with np.full
1 parent f268e3a commit 9de15a0

File tree

7 files changed

+12
-12
lines changed

7 files changed

+12
-12
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3978,7 +3978,7 @@ def dopatch(xs, ys, **kwargs):
39783978
# maybe draw the fliers
39793979
if showfliers:
39803980
# fliers coords
3981-
flier_x = np.ones(len(stats['fliers'])) * pos
3981+
flier_x = np.full(len(stats['fliers']), pos)
39823982
flier_y = stats['fliers']
39833983

39843984
fliers.extend(doplot(

lib/matplotlib/hatch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ 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)
173173
self.shape_codes[0] = Path.MOVETO
174174
Shapes.__init__(self, hatch, density)
175175

lib/matplotlib/legend_handler.py

Lines changed: 1 addition & 1 deletion
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(xdata.shape, ((height - ydescent) / 2.), float)
235235
legline = Line2D(xdata, ydata)
236236

237237
self.update_prop(legline, orig_handle, legend)

lib/matplotlib/path.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def make_compound_path_from_polys(cls, XY):
316316
stride = numsides + 1
317317
nverts = numpolys * stride
318318
verts = np.zeros((nverts, 2))
319-
codes = np.ones(nverts, int) * cls.LINETO
319+
codes = np.full(nverts, cls.LINETO, dtype=int)
320320
codes[0::stride] = cls.MOVETO
321321
codes[numsides::stride] = cls.CLOSEPOLY
322322
for i in range(numsides):
@@ -566,7 +566,7 @@ def interpolated(self, steps):
566566
vertices = simple_linear_interpolation(self.vertices, steps)
567567
codes = self.codes
568568
if codes is not None:
569-
new_codes = Path.LINETO * np.ones(((len(codes) - 1) * steps + 1, ))
569+
new_codes = np.full(((len(codes) - 1) * steps + 1, ), Path.LINETO)
570570
new_codes[0::steps] = codes
571571
else:
572572
new_codes = None
@@ -825,7 +825,7 @@ def unit_circle_righthalf(cls):
825825

826826
float)
827827

828-
codes = cls.CURVE4 * np.ones(14)
828+
codes = np.full(14, cls.CURVE4)
829829
codes[0] = cls.MOVETO
830830
codes[-1] = cls.CLOSEPOLY
831831

@@ -887,7 +887,7 @@ def arc(cls, theta1, theta2, n=None, is_wedge=False):
887887
if is_wedge:
888888
length = n * 3 + 4
889889
vertices = np.zeros((length, 2), float)
890-
codes = cls.CURVE4 * np.ones((length, ), cls.code_type)
890+
codes = np.full((length, ), cls.CURVE4, dtype=cls.code_type)
891891
vertices[1] = [xA[0], yA[0]]
892892
codes[0:2] = [cls.MOVETO, cls.LINETO]
893893
codes[-2:] = [cls.LINETO, cls.CLOSEPOLY]
@@ -896,7 +896,7 @@ def arc(cls, theta1, theta2, n=None, is_wedge=False):
896896
else:
897897
length = n * 3 + 1
898898
vertices = np.empty((length, 2), float)
899-
codes = cls.CURVE4 * np.ones((length, ), cls.code_type)
899+
codes = np.full((length, ), cls.CURVE4, dtype=cls.code_type)
900900
vertices[0] = [xA[0], yA[0]]
901901
codes[0] = cls.MOVETO
902902
vertex_offset = 1

lib/matplotlib/tri/triinterpolate.py

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

lib/matplotlib/tri/trirefine.py

Lines changed: 2 additions & 2 deletions
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,
@@ -243,7 +243,7 @@ def _refine_triangulation_once(triangulation, ancestors=None):
243243
np.arange(ntri, dtype=np.int32)]))
244244
edge_apexes = np.ravel(np.vstack([np.zeros(ntri, dtype=np.int32),
245245
np.ones(ntri, dtype=np.int32),
246-
np.ones(ntri, dtype=np.int32)*2]))
246+
np.full(ntri, 2, dtype=np.int32)]))
247247
edge_neighbors = neighbors[edge_elems, edge_apexes]
248248
mask_masters = (edge_elems > edge_neighbors)
249249

lib/matplotlib/tri/tritools.py

Lines changed: 1 addition & 1 deletion
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)