I'm trying to produce a 3D scatter plot in mayavi. However, the scales of the input data are very different. See a test example below:
x = np.linspace(0, 1, 100)
y = np.linspace(0, 100, 100)
x, y = np.meshgrid(x,y)
x = [xi for xj in x for xi in xj]
y = [yi for yj in y for yi in yj]
z = [random.randint(0,10)/10.0 for i in range(10000)]
from mayavi import mlab
s = mlab.points3d(x, y, z, scale_factor = 0.1)
mlab.show()
Now, the output plot is squeezed down into almost a line considering y
axes is very long in comparison with others. How can I make the plot more visually readable, so that axes extents are comparable?
Got it using extent
parameter in quite an unexpected way:
s = mlab.points3d(x, y, z, mode = 'point', extent = [0,1,0,1,0,1])
Apparently, coordinates in mayavi are not really coordinates in "plot" sense, they actually define your 3D object in a window. The extent
then says to scale the whole thing to the extent
values. If they are all set to the same values, you get a square plot. The problem is that if you use spheres, they will also scale and become ellipsoids. That's why I had to use mode = 'point'
here instead.
To add axes with proper marks, you would need to add the following:
mlab.axes(s, ranges = [min(x), max(x), min(y), max(y), min(z), max(z)])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With