From ef5689faafabf5aba9ea71010a0ac1f79e6ae326 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Mon, 21 Aug 2023 12:59:08 -0500 Subject: [PATCH 1/2] MAINT: Numpy 2.0 deprecations for row_stack and in1d Part of #26422, xref numpy/numpy#24445 --- lib/matplotlib/axes/_axes.py | 6 +++--- lib/matplotlib/axes/_base.py | 2 +- lib/matplotlib/contour.py | 6 +++--- lib/matplotlib/projections/polar.py | 2 +- lib/matplotlib/stackplot.py | 2 +- lib/matplotlib/tests/test_triangulation.py | 2 +- lib/mpl_toolkits/mplot3d/art3d.py | 4 ++-- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 036d4f284fe5..4bd1e83d2f5a 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3674,7 +3674,7 @@ def apply_mask(arrays, mask): # elow, ehigh = np.broadcast_to(...) # return dep - elow * ~lolims, dep + ehigh * ~uplims # except that broadcast_to would strip units. - low, high = dep + np.row_stack([-(1 - lolims), 1 - uplims]) * err + low, high = dep + np.vstack([-(1 - lolims), 1 - uplims]) * err barcols.append(lines_func( *apply_mask([indep, low, high], everymask), **eb_lines_style)) if self.name == "polar" and dep_axis == "x": @@ -5471,8 +5471,8 @@ def get_interp_point(idx): collection = mcoll.PolyCollection(polys, **kwargs) # now update the datalim and autoscale - pts = np.row_stack([np.column_stack([ind[where], dep1[where]]), - np.column_stack([ind[where], dep2[where]])]) + pts = np.vstack([np.column_stack([ind[where], dep1[where]]), + np.column_stack([ind[where], dep2[where]])]) if ind_dir == "y": pts = pts[:, ::-1] diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 3796d9bbe508..b55ce7e99886 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2403,7 +2403,7 @@ def _update_patch_limits(self, patch): vertices.append(curve([0, *dzeros, 1])) if len(vertices): - vertices = np.row_stack(vertices) + vertices = np.vstack(vertices) patch_trf = patch.get_transform() updatex, updatey = patch_trf.contains_branch_seperately(self.transData) diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index 79f66b896131..f148d35618dd 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -511,13 +511,13 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5): if closed: # This will remove contour if shorter than label if all(i != -1 for i in I): - nlc.append(np.row_stack([xy2, lc[I[1]:I[0]+1], xy1])) + nlc.append(np.vstack([xy2, lc[I[1]:I[0]+1], xy1])) else: # These will remove pieces of contour if they have length zero if I[0] != -1: - nlc.append(np.row_stack([lc[:I[0]+1], xy1])) + nlc.append(np.vstack([lc[:I[0]+1], xy1])) if I[1] != -1: - nlc.append(np.row_stack([xy2, lc[I[1]:]])) + nlc.append(np.vstack([xy2, lc[I[1]:]])) # The current implementation removes contours completely # covered by labels. Uncomment line below to keep diff --git a/lib/matplotlib/projections/polar.py b/lib/matplotlib/projections/polar.py index e4a99a01a774..0bff320e5728 100644 --- a/lib/matplotlib/projections/polar.py +++ b/lib/matplotlib/projections/polar.py @@ -121,7 +121,7 @@ def transform_path_non_affine(self, path): codes.extend(arc.codes[1:]) else: # Interpolate. trs = cbook.simple_linear_interpolation( - np.row_stack([(last_t, last_r), trs]), + np.vstack([(last_t, last_r), trs]), path._interpolation_steps)[1:] xys.extend(self.transform_non_affine(trs)) codes.extend([Path.LINETO] * len(trs)) diff --git a/lib/matplotlib/stackplot.py b/lib/matplotlib/stackplot.py index c97a21e029f9..2629593683e5 100644 --- a/lib/matplotlib/stackplot.py +++ b/lib/matplotlib/stackplot.py @@ -68,7 +68,7 @@ def stackplot(axes, x, *args, stacked area plot. """ - y = np.row_stack(args) + y = np.vstack(args) labels = iter(labels) if colors is not None: diff --git a/lib/matplotlib/tests/test_triangulation.py b/lib/matplotlib/tests/test_triangulation.py index 682a0fbe4b75..14c591abd4e5 100644 --- a/lib/matplotlib/tests/test_triangulation.py +++ b/lib/matplotlib/tests/test_triangulation.py @@ -1035,7 +1035,7 @@ def test_trirefine(): x_verif, y_verif = np.meshgrid(x_verif, x_verif) x_verif = x_verif.ravel() y_verif = y_verif.ravel() - ind1d = np.in1d(np.around(x_verif*(2.5+y_verif), 8), + ind1d = np.isin(np.around(x_verif*(2.5+y_verif), 8), np.around(x_refi*(2.5+y_refi), 8)) assert_array_equal(ind1d, True) diff --git a/lib/mpl_toolkits/mplot3d/art3d.py b/lib/mpl_toolkits/mplot3d/art3d.py index a41485f62529..ac6e841f5019 100644 --- a/lib/mpl_toolkits/mplot3d/art3d.py +++ b/lib/mpl_toolkits/mplot3d/art3d.py @@ -942,8 +942,8 @@ def set_zsort(self, zsort): def get_vector(self, segments3d): """Optimize points for projection.""" if len(segments3d): - xs, ys, zs = np.row_stack(segments3d).T - else: # row_stack can't stack zero arrays. + xs, ys, zs = np.vstack(segments3d).T + else: # vstack can't stack zero arrays. xs, ys, zs = [], [], [] ones = np.ones(len(xs)) self._vec = np.array([xs, ys, zs, ones]) From ebef650cd7741853b4f23265c024ef622b17eda6 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Wed, 23 Aug 2023 16:01:00 -0500 Subject: [PATCH 2/2] Switch to hstack for consistency --- lib/matplotlib/axes/_axes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 4bd1e83d2f5a..93a60c332cf4 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -5471,8 +5471,8 @@ def get_interp_point(idx): collection = mcoll.PolyCollection(polys, **kwargs) # now update the datalim and autoscale - pts = np.vstack([np.column_stack([ind[where], dep1[where]]), - np.column_stack([ind[where], dep2[where]])]) + pts = np.vstack([np.hstack([ind[where, None], dep1[where, None]]), + np.hstack([ind[where, None], dep2[where, None]])]) if ind_dir == "y": pts = pts[:, ::-1]