Mayavi - color vectors based on direction instead of magnitude

1.1k views Asked by At

I've been using Mayavi to make some very nice vector plots lately. Using Numpy, I read in data from file (which is organized in columns as 'x y z Vx Vy Vz') and then plot the results using Mayavi:

#Import libraries
from mayavi import mlab
import numpy as np

#Read in data
data = np.loadtxt('filename', float)
x = data[:,0]
y = data[:,1]
z = data[:,2]
Vx = data[:,3]
Vy = data[:,4]
Vz = data[:,5]

#Plot
mlab.quiver3d(x, y, z, Vx, Vy, Vz, mode='arrow', colormap = 'jet')
mlab.show()

This worked fine until I needed to plot normalized vectors. Namely, the magnitude of all the vectors is 1. The quiver3d function gives a vector its color based on its magnitude, so right now every vector is one color. However, the information I need to convey is based on orientation of the vectors.

I'd love if someone could help me figure out how to color vectors in Mayavi using the vector's direction and NOT its magnitude.

1

There are 1 answers

1
aestrivex On

I don't believe mayavi has a setting to do this easily. But you can do it by passing scalar values corresponding to the color

x,y,z,u,v,w = data.T
scalars = np.random.random((len(data),))
mlab.quiver3d(x,y,z,u,v,w, scalars=scalars)

Now if you want some meaningful color scheme, like R=X, G=Y, B=Z or similar, mayavi isn't designed to do this very well. But see How to directly set RGB/RGBA colors in mayavi