Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

10,000+ Point 3D Scatter Plots in Python (with Quick Rendering)

Performance-wise, the following code snippet works perfectly fine for me when plotting in mayavi.

import numpy as np
from mayavi import mlab

n = 5000
x = np.random.rand(n)
y = np.random.rand(n)
z = np.random.rand(n)
s = np.sin(x)**2 + np.cos(y)

mlab.points3d(x, y, z, s, colormap="RdYlBu", scale_factor=0.02, scale_mode='none')

But mayavi begins to choke once n >= 10000. The analogous 3d plotting routine in matplotlib, (Axes3D.scatter) similarly struggles with data sets of this size (why I started looking into mayavi in the first place).

First, is there something in mayavi (trivial or nontrivial) that I am missing that would make 10,000+ point scatter plots much easier to render?

Second, if the answer above is no, what other options (either in mayavi or a different python package) do I have to plot datasets of this magnitude?

I tagged ParaView simply to add that rendering my data in ParaView goes super smoothly, leading me to believe that I am not trying to do anything unreasonable.

Update:

Specifying the mode as a 2D glyph goes a long way towards speeding things up. E.g.

mlab.points3d(x, y, z, s, colormap="RdYlBu", scale_factor=0.02,
              scale_mode='none', mode='2dcross')

can easily support up to 100,000 points

enter image description here

It would still be nice if anyone could add some info about how to speed up the rendering of 3D glyphs.

like image 325
lanery Avatar asked Aug 15 '16 19:08

lanery


People also ask

How do you make a 3D scatterplot in Python?

Generally 3D scatter plot is created by using ax. scatter3D() the function of the matplotlib library which accepts a data sets of X, Y and Z to create the plot while the rest of the attributes of the function are the same as that of two dimensional scatter plot.

How many points can Matplotlib handle?

Interesting. As Jonathan Dursi's answer mentions, 20 million points is achievable with Matplotlib, but with some constraints (raster output,…).

How do you make a 3 dimensional scatter plot?

Create ChartAfter adding data, go to the 'Traces' section under the 'Structure' menu on the left-hand side. Choose the 'Type' of trace, then choose '3D Scatter' under '3D' chart type. Next, select 'X', 'Y' and 'Z' values from the dropdown menus. This will create a 3D scatter trace, as seen below.


1 Answers

PyQtGraph is a much more performant plotting package, although not as "beautiful" as matplotlib or mayavi. It is made for number crunching and should therefore easily render points in the order of ten thousands.

As for mayavi and matplotlib: I think with that number of points you've reached what is possible with those packages.

Edit: VisPy seems to be the successor to PyQtGraph and some other visualization packages. Might be a bit overkill, but it can display a few hundred thousand points easily by offloading computation to the GPU.

like image 62
Ian Avatar answered Oct 19 '22 11:10

Ian