在Matlab中如何更改箭头图中的箭头样式?

Iso*_*ion 15 matlab plot vector

我想在箭头图中更改默认的箭头样式.我该怎么改变它?

mar*_*sei 19

对于Matlab版本> R2014b

自R2014b版本以来,Matlab已经修改了其图形组件的结构.这是使用Matlab 注释的最新代码.

在此输入图像描述

由...产生

headWidth = 8;
headLength = 8;
LineLength = 0.08;

%some data
[x,y] = meshgrid(0:0.2:2,0:0.2:2);
u = cos(x).*y;
v = sin(x).*y;

%quiver plots
figure('Position',[10 10 1000 600],'Color','w');
hax_1 = subplot(1,2,1);
hq = quiver(x,y,u,v);           %get the handle of quiver
title('Regular Quiver plot','FontSize',16);

%get the data from regular quiver
U = hq.UData;
V = hq.VData;
X = hq.XData;
Y = hq.YData;

%right version (with annotation)
hax_2 = subplot(1,2,2);
%hold on;
for ii = 1:length(X)
    for ij = 1:length(X)

        headWidth = 5;
        ah = annotation('arrow',...
            'headStyle','cback1','HeadLength',headLength,'HeadWidth',headWidth);
        set(ah,'parent',gca);
        set(ah,'position',[X(ii,ij) Y(ii,ij) LineLength*U(ii,ij) LineLength*V(ii,ij)]);

    end
end
%axis off;
title('Quiver - annotations ','FontSize',16);

linkaxes([hax_1 hax_2],'xy');
Run Code Online (Sandbox Code Playgroud)

请注意,这段代码改变了头部样式并控制了线条的长度(在左侧面板中,您可以看到左侧子图的左上部分重叠箭头,而右侧子图则没有).箭头的长度和宽度不会被修改.

对于这个编辑,我没有保留为角度编码的颜色方案,并丢弃动态头部大小.它让事情变得更加清晰.


对于Matlab版本<R2014b

箭头图很难修改.正如@Luis Mendo所说,你可以在matlab安装中修改箭头功能.但是,您仍然会受到以编程方式绘制具有漂亮补丁/线条的箭头的复杂性的限制.可能有一个更容易使用的路线annotation- 请参阅设置headStyle属性的"箭头 - 注释"子图cback1.

注释是图形对象(线条,文本框,箭头......),一旦绘图完成,您可以手动插入.例如,它们显示附加文本或指向特定区域.您也可以通过定义它们的位置以编程方式插入它们 - 这是我们将采取的选项.我们首先绘制常规quiver图(左侧面板),获取蓝线XY数据,然后使用这些坐标插入注释箭头,每个箭头都显示在完全相同的位置(相同位置,相同角度,相同尺寸;右侧面板) ).

注释箭头有一些很不错的属性,你可以很容易地修改,例如Color,HeadWidth,HeadLength,和HeadStyle.在下图中,我根据角度与x轴的角度修改了每个箭头的颜色,headWidth这取决于长度.

如下图

在此输入图像描述

由...产生

%some data
[x,y] = meshgrid(0:0.2:2,0:0.2:2);
u = cos(x).*y;
v = sin(x).*y;

%quiver plots
figure('Position',[10 10 1000 600],'Color','w');
hax_1 = subplot(1,2,1);

%left version (regular)
hq1 = quiver(x,y,u,v);

%get the line position (first handle)
hkid = get(hq1,'children');
X = get(hkid(1),'XData');
Y = get(hkid(1),'YData');
axis off;
title('Quiver - regular ','FontSize',16);

%right version (with annotation)
hax_2 = subplot(1,2,2);
cmap = jet(116); %colormap, 116 because angles goes up to 115 degrees

for ii = 1:3:length(X)-1

    headWidth = 200 * sqrt((X(ii+1)-X(ii)).^2 + (Y(ii+1)-Y(ii)).^2); % set the headWidth, function of length of arrow
    angled = floor(atan2(Y(ii+1)-Y(ii),X(ii+1)-X(ii))*180/pi) + 1; %get the angle
    ah = annotation('arrow',...
        'Color', cmap(angled,:),...
        'headStyle','cback1','HeadLength',50,'HeadWidth',headWidth);
    set(ah,'parent',gca);
    set(ah,'position',[X(ii) Y(ii) X(ii+1)-X(ii) Y(ii+1)-Y(ii)]);
end
axis off;
title('Quiver - annotations ','FontSize',16);

linkaxes([hax_1 hax_2],'xy');
Run Code Online (Sandbox Code Playgroud)


Lui*_*ndo 7

该文件refresh.m位于文件夹中...\MATLAB\...\toolbox\matlab\specgraph\@specgraph\@quivergroup\@quivergroup包含以下行:

%// Arrow head parameters
alpha = .33;  %// Size of arrow head relative to the length of the vector
beta = .25;  %// Width of the base of the arrow head relative to the length
Run Code Online (Sandbox Code Playgroud)

改变数值alphabeta达到预期效果.

但是,这需要修改Matlab的文件,因此不建议这样做.如果这样做,请保留原始refresh.m文件的副本.


结果使用quiver帮助中显示的示例代码:

[x,y] = meshgrid(-2:.2:2,-1:.15:1);
z = x .* exp(-x.^2 - y.^2); [px,py] = gradient(z,.2,.15);
quiver(x,y,px,py), hold off, axis image
Run Code Online (Sandbox Code Playgroud)
  • 使用原始参数(alpha = .33; beta = .25;):

    在此输入图像描述

  • alpha = .5; beta = .5;:

    在此输入图像描述