Skip to content

Commit 67dea11

Browse files
committed
np.concatenate cleanups.
Replace np.concatenate by iterable unpacking when the arrays are short (so performance doesn't matter) and legibility is improved, or by np.roll(), or by np.append.
1 parent fc413b3 commit 67dea11

File tree

6 files changed

+21
-43
lines changed

6 files changed

+21
-43
lines changed

examples/specialty_plots/radar_chart.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ def _close_line(self, line):
6666
x, y = line.get_data()
6767
# FIXME: markers at x[0], y[0] get doubled-up
6868
if x[0] != x[-1]:
69-
x = np.concatenate((x, [x[0]]))
70-
y = np.concatenate((y, [y[0]]))
69+
x = np.append(x, x[0])
70+
y = np.append(y, y[0])
7171
line.set_data(x, y)
7272

7373
def set_varlabels(self, labels):

lib/matplotlib/bezier.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,14 @@ def split_path_inout(path, inside, tolerance=0.01, reorder_inout=False):
268268

269269
ctl_points_old = ctl_points
270270

271-
concat = np.concatenate
272-
273271
iold = 0
274272
i = 1
275273

276274
for ctl_points, command in path_iter:
277275
iold = i
278276
i += len(ctl_points) // 2
279277
if inside(ctl_points[-2:]) != begin_inside:
280-
bezier_path = concat([ctl_points_old[-2:], ctl_points])
278+
bezier_path = np.concatenate([ctl_points_old[-2:], ctl_points])
281279
break
282280
ctl_points_old = ctl_points
283281
else:
@@ -302,15 +300,15 @@ def split_path_inout(path, inside, tolerance=0.01, reorder_inout=False):
302300
verts_right = right[:]
303301

304302
if path.codes is None:
305-
path_in = Path(concat([path.vertices[:i], verts_left]))
306-
path_out = Path(concat([verts_right, path.vertices[i:]]))
303+
path_in = Path(np.concatenate([path.vertices[:i], verts_left]))
304+
path_out = Path(np.concatenate([verts_right, path.vertices[i:]]))
307305

308306
else:
309-
path_in = Path(concat([path.vertices[:iold], verts_left]),
310-
concat([path.codes[:iold], codes_left]))
307+
path_in = Path(np.concatenate([path.vertices[:iold], verts_left]),
308+
np.concatenate([path.codes[:iold], codes_left]))
311309

312-
path_out = Path(concat([verts_right, path.vertices[i:]]),
313-
concat([codes_right, path.codes[i:]]))
310+
path_out = Path(np.concatenate([verts_right, path.vertices[i:]]),
311+
np.concatenate([codes_right, path.codes[i:]]))
314312

315313
if reorder_inout and not begin_inside:
316314
path_in, path_out = path_out, path_in

lib/matplotlib/mlab.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,8 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
572572

573573
if sides == 'twosided':
574574
# center the frequency range at zero
575-
freqs = np.concatenate((freqs[freqcenter:], freqs[:freqcenter]))
576-
result = np.concatenate((result[freqcenter:, :],
577-
result[:freqcenter, :]), 0)
575+
freqs = np.roll(freqs, -freqcenter, axis=0)
576+
result = np.roll(result, -freqcenter, axis=0)
578577
elif not pad_to % 2:
579578
# get the last value correctly, it is negative otherwise
580579
freqs[-1] *= -1

lib/matplotlib/patches.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,8 +2342,7 @@ def transmute(self, x0, y0, width, height, mutation_size):
23422342
width, height,
23432343
mutation_size)
23442344
# Add a trailing vertex to allow us to close the polygon correctly
2345-
saw_vertices = np.concatenate([np.array(saw_vertices),
2346-
[saw_vertices[0]]], axis=0)
2345+
saw_vertices = np.concatenate([saw_vertices, [saw_vertices[0]]])
23472346
codes = ([Path.MOVETO] +
23482347
[Path.CURVE3, Path.CURVE3] * ((len(saw_vertices)-1)//2) +
23492348
[Path.CLOSEPOLY])

lib/matplotlib/tests/test_ticker.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,7 @@ def test_minor(self, lims, expected_low_ticks):
357357
else:
358358
# subsample
359359
_LogitHelper.assert_almost_equal(
360-
np.sort(np.concatenate((major_ticks, minor_ticks))),
361-
expected_ticks,
362-
)
360+
sorted([*major_ticks, *minor_ticks]), expected_ticks)
363361

364362
def test_minor_attr(self):
365363
loc = mticker.LogitLocator(nbins=100)

lib/mpl_toolkits/axisartist/angle_helper.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,11 @@ def select_step_degree(dv):
2020
second_limits_ = np.array(minsec_limits_) / 3600
2121
second_factors = [3600.] * len(second_limits_)
2222

23-
degree_limits = np.concatenate([second_limits_,
24-
minute_limits_,
25-
degree_limits_])
23+
degree_limits = [*second_limits_, *minute_limits_, *degree_limits_]
24+
degree_steps = [*minsec_steps_, *minsec_steps_, *degree_steps_]
25+
degree_factors = [*second_factors, *minute_factors, *degree_factors]
2626

27-
degree_steps = np.concatenate([minsec_steps_,
28-
minsec_steps_,
29-
degree_steps_])
30-
31-
degree_factors = np.concatenate([second_factors,
32-
minute_factors,
33-
degree_factors])
34-
35-
n = degree_limits.searchsorted(dv)
27+
n = np.searchsorted(degree_limits, dv)
3628
step = degree_steps[n]
3729
factor = degree_factors[n]
3830

@@ -54,19 +46,11 @@ def select_step_hour(dv):
5446
second_limits_ = np.array(minsec_limits_) / 3600
5547
second_factors = [3600.] * len(second_limits_)
5648

57-
hour_limits = np.concatenate([second_limits_,
58-
minute_limits_,
59-
hour_limits_])
60-
61-
hour_steps = np.concatenate([minsec_steps_,
62-
minsec_steps_,
63-
hour_steps_])
64-
65-
hour_factors = np.concatenate([second_factors,
66-
minute_factors,
67-
hour_factors])
49+
hour_limits = [*second_limits_, *minute_limits_, *hour_limits_]
50+
hour_steps = [*minsec_steps_, *minsec_steps_, *hour_steps_]
51+
hour_factors = [*second_factors, *minute_factors, *hour_factors]
6852

69-
n = hour_limits.searchsorted(dv)
53+
n = np.searchsorted(hour_limits, dv)
7054
step = hour_steps[n]
7155
factor = hour_factors[n]
7256

0 commit comments

Comments
 (0)