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

5 views (last 30 days)
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

Answers (1)

Gautam
Gautam on 23 Oct 2024
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

Categories

Find more on Vector Fields in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!