How can I plot a velocity field plot with color code?

조회 수: 9 (최근 30일)
Emre Karaoglan
Emre Karaoglan 2014년 4월 11일
답변: Gautam 2024년 10월 23일
Hello,
I want to plot the velocity field of my data with color code according to the vector magnitude.
I got my nx1 vectors for x,y,vx,vy which represent x position, y position, x direction velocity and y direction velocity. I am using
figure quiver(x,y,vx,vy);
My question is, should I be using some other function to plot the color coded velocity field or does the function 'quiver' have the feature of color code?
Thanks

답변 (1개)

Gautam
Gautam 2024년 10월 23일
Hello Emre
The quiver function in MATLAB does not natively support color coding based on vector magnitude. However, you can achieve this by plotting each vector individually and setting the color according to its magnitude
[x,y] = meshgrid(-pi:pi/8:pi,-pi:pi/8:pi);
vx = sin(y);
vy = cos(x);
magnitude = sqrt(vx.^2 + vy.^2);
magnitude_normalized = (magnitude - min(magnitude)) ./ (max(magnitude) - min(magnitude));
cmap = jet(256);
figure;
hold on;
for i = 1:length(x)
for j=1:length(y)
% Determine color index
color_idx = round(magnitude_normalized(i,j) * (length(cmap) - 1)) + 1;
% Plot vector with color
quiver(x(i,j), y(i,j), vx(i,j), vy(i,j),1, 'Color', cmap(color_idx, :));
end
end
hold off;
colorbar;
colormap(cmap);
This produces the following output

카테고리

Help CenterFile Exchange에서 Vector Fields에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by