Title: [MNT] Switch from `np.column_stack()` to `np.vstack().T` for performance · Issue #31130 · matplotlib/matplotlib · GitHub
Open Graph Title: [MNT] Switch from `np.column_stack()` to `np.vstack().T` for performance · Issue #31130 · matplotlib/matplotlib
X Title: [MNT] Switch from `np.column_stack()` to `np.vstack().T` for performance · Issue #31130 · matplotlib/matplotlib
Description: From the discussion in #31001 (comment), it looks like np.column_stack() is generally a slow operation compared to np.vstack().T. This is because the former has to interleave elements in memory whereas the second does contiguous memory c...
Open Graph Description: From the discussion in #31001 (comment), it looks like np.column_stack() is generally a slow operation compared to np.vstack().T. This is because the former has to interleave elements in memory whe...
X Description: From the discussion in #31001 (comment), it looks like np.column_stack() is generally a slow operation compared to np.vstack().T. This is because the former has to interleave elements in memory whe...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/31130
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[MNT] Switch from `np.column_stack()` to `np.vstack().T` for performance","articleBody":"From the discussion in https://github.com/matplotlib/matplotlib/pull/31001#discussion_r2790413429, it looks like `np.column_stack()` is generally a slow operation compared to `np.vstack().T`. This is because the former has to interleave elements in memory whereas the second does contiguous memory copies and returns a view.\n\nIt's unclear to me how many of these operations are driving time spent in the hot paths of the code, but we should see a performance improvement by switching things over.\n\nMarking this as an easy first issue since it's largely a find-and-replace.\n\n```\n10,000 elements: 10 runs x 10,000 iterations\n\nWith broadcast:\n- `np.column_stack(np.broadcast_arrays(x, y))`: 36.47 us\n- `np.vstack(np.broadcast_arrays(x, y)).T`: 27.67 us\n- `np.empty + assign`: 30.09 us\n\nWithout broadcast:\n- `np.column_stack([x, y])`: 20.63 us\n- `np.vstack((x, y)).T`: 13.18 us\n```\n\n```python\n\"\"\"Benchmark different array combination methods.\"\"\"\n\nimport timeit\nimport numpy as np\n\nN = 10_000\nNUMBER = 10_000\nREPEAT = 10\n\nx = np.linspace(0, 1, N)\ny = np.float64(2.0)\n\n# Pre-matched arrays for non-broadcast cases\nx_full = x\ny_full = np.full(N, 2.0)\n\n\ndef broadcast_column_stack():\n return np.column_stack(np.broadcast_arrays(x, y))\n\ndef broadcast_vstack_T():\n return np.vstack(np.broadcast_arrays(x, y)).T\n\ndef broadcast_empty_assign():\n out = np.empty((N, 2))\n bx, by = np.broadcast_arrays(x, y)\n out[:, 0] = bx\n out[:, 1] = by\n return out\n\ndef no_broadcast_column_stack():\n return np.column_stack([x_full, y_full])\n\ndef no_broadcast_vstack_T():\n return np.vstack((x_full, y_full)).T\n\n\ndef bench(func):\n times = timeit.repeat(func, number=NUMBER, repeat=REPEAT)\n return min(times) / NUMBER\n\n\nif __name__ == \"__main__\":\n print(f\"{N:,} elements: {REPEAT} runs x {NUMBER:,} iterations\")\n\n broadcast_cases = [\n (\"np.column_stack(np.broadcast_arrays(x, y))\", broadcast_column_stack),\n (\"np.vstack(np.broadcast_arrays(x, y)).T\", broadcast_vstack_T),\n (\"np.empty + assign\", broadcast_empty_assign),\n ]\n\n no_broadcast_cases = [\n (\"np.column_stack([x, y])\", no_broadcast_column_stack),\n (\"np.vstack((x, y)).T\", no_broadcast_vstack_T),\n ]\n\n print(\"\\nWith broadcast:\")\n for label, func in broadcast_cases:\n t = bench(func)\n print(f\"- `{label}`: {t * 1e6:.2f} us\")\n\n print(\"\\nWithout broadcast:\")\n for label, func in no_broadcast_cases:\n t = bench(func)\n print(f\"- `{label}`: {t * 1e6:.2f} us\")\n```","author":{"url":"https://github.com/scottshambaugh","@type":"Person","name":"scottshambaugh"},"datePublished":"2026-02-10T21:53:30.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":10},"url":"https://github.com/31130/matplotlib/issues/31130"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:01fb3ca5-4cad-9c26-d5f7-f61059b80f30 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 83A4:15C919:1CC84E:26FA00:6A52554D |
| html-safe-nonce | cda4d8da6520bf7934339793ddfc4087a5d41de747939cacd6f76af28031303f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4M0E0OjE1QzkxOToxQ0M4NEU6MjZGQTAwOjZBNTI1NTREIiwidmlzaXRvcl9pZCI6IjUxMjM1MTA2OTI5ODU1OTExMTciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9835d503b7ffff0e22b20f84c38f091dfa1228403212a9dd9e5b9560f0dbe470 |
| hovercard-subject-tag | issue:3923735898 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/31130/issue_layout |
| twitter:image | https://opengraph.githubassets.com/972fb44824ae7b5a201cca585b042fc5b19527d8374eb406881ab2e1d4405868/matplotlib/matplotlib/issues/31130 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/972fb44824ae7b5a201cca585b042fc5b19527d8374eb406881ab2e1d4405868/matplotlib/matplotlib/issues/31130 |
| og:image:alt | From the discussion in #31001 (comment), it looks like np.column_stack() is generally a slow operation compared to np.vstack().T. This is because the former has to interleave elements in memory whe... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | scottshambaugh |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width