How can I change vector colors in quiver plots and add color bar based on vector magnitudes and custom magnitudes assigned to each vector?

13 ビュー (過去 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!

採用された回答

Gautam
Gautam 2024 年 10 月 23 日
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
  1 件のコメント
Nafiz Sheikh
Nafiz Sheikh 2024 年 10 月 25 日
Thanks! Your solution is 9 years too late for me, but hopefully it'll help someone else

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeVector Fields についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by