
How can I change vector colors in quiver plots and add color bar based on vector magnitudes and custom magnitudes assigned to each vector?
조회 수: 1 (최근 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일
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!