How can I plot a velocity field plot with color code?
12 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Gautam
2024-10-23,4:39
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Vector Fields 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!