…s only) Per issue matplotlib#31130, np.column_stack is slower than np.vstack().T because it has to interleave elements in memory whereas np.vstack().T does contiguous memory copies and returns a view. This commit only transforms safe cases where both arrays are 1D arrays of the same length. Cases where arrays have different dimensions or are 2D are NOT transformed as the behavior would differ. Benchmark results from issue: - With broadcast: np.column_stack -> 36.47 us, np.vstack().T -> 27.67 us - Without broadcast: np.column_stack -> 20.63 us, np.vstack().T -> 13.18 us Changes: - lib/matplotlib/lines.py: Line2D.recache() - both x and y are raveled to 1D - lib/matplotlib/path.py: Path.unit_regular_polygon() - cos/sin are both 1D - lib/matplotlib/patches.py: StepPatch - x and y are both 1D arrays Related: matplotlib#31130
When replacing np.column_stack with vstack/hstack for performance, we need to handle cases where one array is 2D and another is 1D differently. For cases like: np.column_stack([c, np.ones(len(c))]) where c is (19, 3) The correct replacement is: np.hstack([c, np.ones(len(c)).reshape(-1, 1)]) For cases where all arrays are 1D: np.column_stack([a, b, c]) where all are 1D The correct replacement is: np.vstack([a, b, c]).T This fixes the build error in colors.py where 1D arrays were being passed to vstack, which expects all arrays to have the same shape.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters