Search code examples
algorithmvectorfigurepolar-coordinates

Color Coding a Vector Field


I have a 2D vector field, and I would like to color code the vector field so that each vector direction shows up in a different color. Say my data are DataX and DataY. Currently, I am doing (in MATLAB):

R = DataX.^2 + DataY.^2;
theta1 = acos( DataX ./ R );
theta2 = asin( DataY ./ R );
surf(x,y,theta1); colormap jet; shading interp
figure; surf(x,y,theta2); colormap jet; shading interp

The issue I am having is that I cannot distinguish between vectors where either the x or y component is zero. For example, the color bar scale looks like (left, using arcsin, and right using arcos). When using arcsin, when DataY is zero (horizontal vector), arcsin(0) gives 0 regardless if the vector is pointing left or right.

enter image description here

Using the arctangent or arccotangent definitions of polar coordinates gives even worse results due to divide by zero errors. I am looking for an algorithm that would let me distinguish between the seemingly degenerate vectors. I have tried combining the arccos and arcsin results, but I have not been able to find a good way to do so.

I am also wondering how to extend this concept to 3D.

Thanks!


Solution

  • For 2D, you can use atan2(DataY, DataX) to get an angle, and then map the angle to your color. Use a cyclic colormap like 'hsv'

    For 3D, you can normalize the vector to unit length and map the 3 components to red, green, and blue.