how to get a plot quiver with velocity magnitude

13 次查看(过去 30 天)
How to get a quiver plot with change in color basing on velocity vector magnitude

回答(2 个)

Priyanshu Mishra
Priyanshu Mishra 2019-11-27
Hi Bharathi,
You can use 'color' linespec to change the color of your velocity vectors. For your reference I have written a code. You may take help of it.
[x,y] = meshgrid(0:0.2:2,0:0.2:2);
u = cos(x).*y;
v = sin(x).*y;
figure
quiver(x,y,u,v,'color',[1 0 0])
The above code will give you red color velocity vectors. By changing the linespec for eg. [1 1 0], it will give you yellow color.
  1 个评论
Daniele Morandi
Daniele Morandi 2022-11-25
Hi Priyanshu,
I guess that Bharathi meant that color could change depending on velocity magnitude, similarly to what happen in contour plot.
Colorbar should help reading magnitude related to color, if this is possible

请先登录,再进行评论。


Gautam
Gautam 2024-10-23,4:43
Hello bharathi
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