Different colours for arrows in quiver plot

79 ビュー (過去 30 日間)
Hamed Nobarani
Hamed Nobarani 2019 年 11 月 11 日
回答済み: Hamed Nobarani 2019 年 11 月 11 日
Hi Everyone,
I want to plot arrows with different magnitude and directions in different colors.The code need to calculate the angle of the each vector and fit them in the colormap.
This is what I have wrote but is there anyone that can help me to find the example or any other command or function in matlab that can plot this for me?
for i=1:40
structure(i,:,:)=rombohedral((i-1)*338+1:i*338,:);
X=structure(i,:,4);
Y=structure(i,:,5);
Z=structure(i,:,6);
U=structure(i,:,7);
V=structure(i,:,8);
W=structure(i,:,9);
C=zeros(338,3);
C(1:300,1)=1;
C(300:end,2)=1;
figure(i)
figure(i)
title('2D')
t=num2str(i);
xlabel([t,' ps'])
hold on
for j=1:338
if (U(j)>0)
quiver(X(j),Y(j),U(j),W(j),'color',[1 0 0])
elseif (U(j)<0)
quiver(X(j),Y(j),U(j),W(j),'color',[0 1 1])
elseif (V(j)>0)
quiver(X(j),Y(j),W(j),V(j),'color',[0 1 0])
elseif (V(j)<0)
quiver(X(j),Y(j),W(j),V(j),'color',[1 0 1])
end
end
end
  2 件のコメント
darova
darova 2019 年 11 月 11 日
You don't want to use quiver? Or what do you want quiver do?
Hamed Nobarani
Hamed Nobarani 2019 年 11 月 11 日
I want to use quiver which is corresponding to the magnitude and angle of the vectors and interpolate them in 360 degree colormap.

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

採用された回答

Thorsten
Thorsten 2019 年 11 月 11 日
Create a circular colormap:
cmap = colormap(hsv(360));
In the loop: compute the angle and an index into the colormap from the angle
alpha = atan2(W(j), U(j)); % or V(j), W(j) depending on your elseif clause
Convert angle to index
idx = ceil(rad2deg(alpha));
if alpha < 1 % negative indices and 0 are invalid
idx = idx + 360;
end
set(h, 'Color', cmap(idx,:)) % before, change each quiver command to h = quiver(...)
  3 件のコメント
Thorsten
Thorsten 2019 年 11 月 11 日
Yes.
Hamed Nobarani
Hamed Nobarani 2019 年 11 月 11 日
Thanks,
I have modified my code but I'm getting this error:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in Colormap (line 39)
set(h, 'Color', cmap(idx,:)) % before, change each quiver command to h = quiver(...)
for i=1:40
structure(i,:,:)=rombohedral((i-1)*338+1:i*338,:);
X=structure(i,:,4);
Y=structure(i,:,5);
Z=structure(i,:,6);
U=structure(i,:,7);
V=structure(i,:,8);
W=structure(i,:,9);
C=zeros(338,3);
C(1:300,1)=1;
C(300:end,2)=1;
figure(i)
figure(i)
title('2D')
t=num2str(i);
xlabel([t,' ps'])
hold on
for j=1:338
cmap = colormap(hsv(360));
alpha = atan2(V(j), U(j));
idx = ceil(rad2deg(alpha));
if alpha < 1
idx = idx + 360;
end
h=quiver(X(j),Y(j),U(j),V(j));
set(h, 'Color', cmap(idx,:))
end
end

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

その他の回答 (1 件)

Hamed Nobarani
Hamed Nobarani 2019 年 11 月 11 日
Hi
I have attached the example here:
clc
close all
clear all
deg=linspace(0,2*pi,720);
r=1;
hold on;
for i =1 :720
cmap = colormap(hsv(360));
u(i)=r*cos(deg(i));
v(i) = r*sin(deg(i));
alpha(i) = atan2(v(i), u(i));
idx = ceil(rad2deg(alpha(i)));
if idx < 1
idx = idx + 360;
end
x(i)=0;
y(i)=0;
quiver(x(i),y(i),u(i),v(i),'color',cmap(idx,:))
axis equal
end

カテゴリ

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