How can I change vector colors in quiver plots and add color bar based on vector magnitudes and custom magnitudes assigned to each vector?
15 次查看(过去 30 天)
显示 更早的评论
I am trying to draw vector quiver plots in MATLAB with custom color schemes. By that I mean that I would like for the quiver plot vectors to be colored differently based on the number I assign to each.
Currently, I have several variables for velocity vector characteristics: X, Y, U (velocity vector component in X direction), and V (velocity vector component in Y direction). Does anyone know how I can create a quiver plot that provides:
- Colored vectors based on total velocity magnitude (based on U and V) with a color bar
- Colored vectors based on a custom matrix of magnitudes (not related to velocity) I have ranging from 0-1
Unfortunately I only know basic MATLAB plotting and have no idea how to implement higher level GUI programming for editing plots using handles etc. Any help would be greatly appreciated. Thank you!
0 个评论
采纳的回答
Gautam
2024-10-23,4:46
Hello Nafiz
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. You can follow a similar approach for the custom 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!