在matlab中着色箭矢量

时间:2017-02-26 07:18:35

标签: matlab plot

我有4个向量,前三个是复数,第四个是它们的总和。

我用quiver成功绘制它们,但是,我需要将第4个颜色设为红色。我怎样才能将第4个颜色变为红色?

% vectors I want to plot as rows (XSTART, YSTART) (XDIR, YDIR)
rays = [
  0 0   real(X1) imag(X1) ;
  0 0   real(X2) imag(X2) ;
  0 0   real(X3) imag(X3) ;
  0 0   real(SUM) imag(SUM) ;
] ;

% quiver plot
quiver(rays( :,1 ), rays( :,2 ), rays( :,3 ), rays( :,4 ));

% set interval
axis([-30 30 -30 30]);

或者我应该使用plotv? https://www.mathworks.com/help/nnet/ref/plotv.html

1 个答案:

答案 0 :(得分:1)

handle函数返回的quiver不允许访问每个元素,以便更改其属性,在本例中为颜色。

可能的解决办法,虽然不是很优雅,但可能是:

  • 为箭头绘制整套数据
  • 从轴中移除要更改颜色的元素的uvxy数据
  • 设置hold on
  • 再次绘制整个数据集的quiver
  • 从轴中移除您不想更改颜色的元素的uvxy数据
  • 将所需颜色设置为保留项目

拟议方法的可能实施可能是:

% Generate some data
rays = [
  0 0   rand-0.5 rand-0.5 ;
   0 0  rand-0.5 rand-0.5 ;
   0 0  rand-0.5 rand-0.5 ;
] ;
rays(4,:)=sum(rays)

% Plot the quiver for the whole matrix (to be used to check the results
figure
h_orig=quiver(rays( :,1 ), rays( :,2 ), rays( :,3 ), rays( :,4 ));
grid minor
% Plot the quiver for the whole matrix
figure
% Plot the quiver for the whole set of data
h0=quiver(rays( :,1 ), rays( :,2 ), rays( :,3 ), rays( :,4 ));
% Get the u, v, x, y data
u=get(h0,'udata')
v=get(h0,'vdata')
x=get(h0,'xdata')
y=get(h0,'ydata')
% Delete the data of the last element
set(h0,'udata',u(1:end-1),'vdata',v(1:end-1),'xdata', ...
   x(1:end-1),'ydata',y(1:end-1))
% Set hold on
hold on
% Plot again the quiver for the whole set of data
h0=quiver(rays( :,1 ), rays( :,2 ), rays( :,3 ), rays( :,4 ));
% Delete the u, v, x, y data of the element you do not want to change the
% colour
set(h0,'udata',u(end),'vdata',v(end),'xdata', ...
   x(end),'ydata',y(end))
% Set the desired colour to the remaining object
h0.Color='r'
grid minor

enter image description here

希望这有帮助,

Qapla'