From a3a0c6725bb856e930eda4bc9174b1d6b7cbc27c Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 7 Jan 2020 16:34:20 +0100 Subject: [PATCH] Avoid using np.r_, np.c_. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit They are really slow ``` In [3]: %timeit np.c_[np.arange(10), np.arange(10)] 11.7 µs ± 60.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) In [4]: %timeit np.column_stack([np.arange(10), np.arange(10)]) 3.91 µs ± 154 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) ``` and the alternative are (slightly) more readable (IMO). --- examples/lines_bars_and_markers/filled_step.py | 4 ++-- lib/matplotlib/contour.py | 10 ++++------ lib/matplotlib/tests/test_axes.py | 2 +- lib/matplotlib/transforms.py | 3 ++- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/examples/lines_bars_and_markers/filled_step.py b/examples/lines_bars_and_markers/filled_step.py index 19119a801690..a156665b0d49 100644 --- a/examples/lines_bars_and_markers/filled_step.py +++ b/examples/lines_bars_and_markers/filled_step.py @@ -64,8 +64,8 @@ def filled_hist(ax, edges, values, bottoms=None, orientation='v', bottoms = 0 bottoms = np.broadcast_to(bottoms, values.shape) - values = np.r_[values, values[-1]] - bottoms = np.r_[bottoms, bottoms[-1]] + values = np.append(values, values[-1]) + bottoms = np.append(bottoms, bottoms[-1]) if orientation == 'h': return ax.fill_betweenx(edges, values, bottoms, **kwargs) diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index 1843098155bf..fd14df77bdcd 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -331,11 +331,9 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5): # Check if closed and, if so, rotate contour so label is at edge closed = _is_closed_polygon(slc) if closed: - slc = np.r_[slc[ind:-1], slc[:ind + 1]] - + slc = np.concatenate([slc[ind:-1], slc[:ind + 1]]) if len(lc): # Rotate lc also if not empty - lc = np.r_[lc[ind:-1], lc[:ind + 1]] - + lc = np.concatenate([lc[ind:-1], lc[:ind + 1]]) ind = 0 # Calculate path lengths @@ -494,7 +492,7 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5, # if there isn't a vertex close enough if not np.allclose(xcmin, lc[imin]): # insert new data into the vertex list - lc = np.r_[lc[:imin], np.array(xcmin)[None, :], lc[imin:]] + lc = np.row_stack([lc[:imin], xcmin, lc[imin:]]) # replace the path with the new one paths[segmin] = mpath.Path(lc) @@ -568,7 +566,7 @@ def labels(self, inline, inline_spacing): # functions, this is not necessary and should probably be # eventually removed. if _is_closed_polygon(lc): - slc = np.r_[slc0, slc0[1:2, :]] + slc = np.row_stack([slc0, slc0[1:2]]) else: slc = slc0 diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index e84f08f47423..690f2d9ceff0 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1681,7 +1681,7 @@ def test_bar_pandas_indexed(pd): @image_comparison(['hist_log'], remove_text=True) def test_hist_log(): data0 = np.linspace(0, 1, 200)**3 - data = np.r_[1-data0, 1+data0] + data = np.concatenate([1 - data0, 1 + data0]) fig = plt.figure() ax = fig.add_subplot(111) ax.hist(data, fill=False, log=True) diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index 4a2fe74462f0..667e00bf854c 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -1576,7 +1576,8 @@ def transform_angles(self, angles, pts, radians=False, pushoff=1e-5): if not radians: angles = np.deg2rad(angles) # Move a short distance away - pts2 = pts + pushoff * np.c_[np.cos(angles), np.sin(angles)] + pts2 = pts + pushoff * np.column_stack([np.cos(angles), + np.sin(angles)]) # Transform both sets of points tpts = self.transform(pts) tpts2 = self.transform(pts2)