Sphere
Log in Join
☆ 5

matplotlib scatter with 2 million points takes forever

matplotlib 3.8.2 on a Jupyter server, trying to plot a scatter of 2 million (x,y) points from a pandas DataFrame. plt.scatter() takes about 40 seconds and the resulting PNG is 50MB. I'm on a laptop with 16GB RAM. I've heard of datashader but never used it. Is there a way to just make scatter faster without adding a whole new library? I tried setting rasterized=True which helped the file size but not the render time. Should I be downsampling first? What do people do for this in practice?

⚑ Report

4 replies

☆ 0
M moss ·2026-11-09#1

For 2M points, plt.scatter is the wrong tool - it creates a PathCollection with 2M objects. Use plt.plot(x, y, ',', alpha=0.1) with the pixel marker ',' which is dramatically faster, or hexbin/hist2d for density. rasterized=True helps output size but not construction cost. Datashader is the real answer for millions of points, designed for exactly this. But first try plot with a pixel marker and see if 2M is tolerable.

⚑ Report
☆ 0
P plastic2 ·2026-11-10#2

plt.plot with the ',' marker took 4s and looks reasonable - way better. But overlapping points just look like a solid blob; I want density information. hist2d gives density but loses the axes look I want. Is there a middle ground short of installing datashader?

⚑ Report
☆ 0
N null ·2026-11-13#3

Middle ground: use a 2D histogram and render it as an image with imshow using extent and origin, then overlay your axes - that gives density with proper coordinates and it's fast. Or scatter with a very small 's' and alpha, but with 2M points it'll still be slow. Once you want density for >1M points, datashader or hexbin is the right call. Datashader integrates with matplotlib via datashader.mpl_ext so you can keep matplotlib axes.

⚑ Report
☆ 0
F fern ·2026-11-16#4

Went with hist2d + imshow for density and kept a light scatter overlay of a 50k random sample so individual points are visible. Render time is under a second. Datashader is on my list but for this plot the matplotlib-only approach was enough. Key takeaway: plt.scatter with 2M points is never going to be fast; use plot with ',' marker, hexbin, or a 2D histogram depending on whether you need density.

⚑ Report
Log in to reply.