how to color code a vector field based on the vector direction?

조회 수: 7 (최근 30일)
Masih Jorat
Masih Jorat 2016년 12월 3일
답변: Gautam 2024년 10월 23일
Hello I want to color code a 3D vector field based on the vectors direction and not their magnitude. I appreciate for any help.

답변 (2개)

KSSV
KSSV 2016년 12월 3일
Well calculate the direction and use this data instead of magnitude.
  댓글 수: 1
Masih Jorat
Masih Jorat 2016년 12월 3일
thanks,
I used the quiver3(x,y,z,u,v,k) to plot the vector field, so u,v, and k are my direction 3 by 3 matrices. is there any way to use contourf or image and direction matrices to plot the a color coded vector field on lets say y-z plane? thank you for your help.

댓글을 달려면 로그인하십시오.


Gautam
Gautam 2024년 10월 23일
You can map the direction to colors using a colormap and use the direction cosines or angles to determine the color.
Here's a sample code
% Define a grid
[x, y, z] = meshgrid(linspace(-5, 5, 10), linspace(-5, 5, 10), linspace(-5, 5, 10));
% Define the vector field
Vx = -y;
Vy = x;
Vz = zeros(size(z));
% Calculate the direction angle
theta = atan2(Vy, Vx);
% Normalize the angle to [0, 1] for colormap
theta_normalized = (theta - min(theta(:))) / (max(theta(:)) - min(theta(:)));
cmap = hsv;
figure;
hold on;
% Plot each vector with color corresponding to its direction
for i = 1:numel(x)
% Determine color index
color_idx = round(theta_normalized(i) * (size(cmap, 1) - 1)) + 1;
% Plot vector
quiver3(x(i), y(i), z(i), Vx(i), Vy(i), Vz(i), 'Color', cmap(color_idx, :), 'AutoScale', 'on');
end
% Set colorbar and colormap
colormap(cmap);
colorbar;
xlabel('X');
ylabel('Y');
zlabel('Z');
axis equal;
hold off;

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by