Different colours for arrows in quiver plot
61 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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(...)
更多回答(1 个)
另请参阅
类别
在 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!