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

时间:2013-09-13 00:05:27

标签: matlab vector plot

我想更改箭头图中的默认箭头样式。我怎样才能改变它?

5 个答案:

答案 0 :(得分:19)

对于Matlab版本> R2014b

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

enter image description here

生成
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');

请注意,这段代码会更改头部样式和控制线的长度(在左侧面板中,您可以看到左侧子图的左上部分重叠箭头,而不是正确的子情节)。箭头的长度和宽度不会被修改。

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


对于Matlab版本< R2014b

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

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

注释箭头具有一些您可以轻松修改的不错属性,例如ColorHeadWidthHeadLengthHeadStyle。在下图中,我修改了每个箭头的颜色,具体取决于它与x轴的角度,以及headWidth取决于长度。

以下图片

enter image description here

生成
%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');

答案 1 :(得分: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

更改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
  • 使用原始参数(alpha = .33; beta = .25;):

    enter image description here

  • 使用alpha = .5; beta = .5;

    enter image description here

答案 2 :(得分:4)

你可以从这里开始:

http://www.mathworks.com/help/matlab/ref/quiver.html

然后你可以在这里查找quiver的可用属性:

http://www.mathworks.com/help/matlab/ref/quivergroupproperties.html

例如,属性MaxHeadSize允许更改箭头的大小。

编辑:此链接中的更多信息:Arrow properties

在底部阅读,其中说

  

您可以选择注释,然后选择显示M代码以获取   您可以在函数或脚本中插入以重现的代码段   注释。

答案 3 :(得分:1)

pablo1977的答案对我来说是最有启发性的。我设法通过调整箭头组属性来获得更大的箭头,即通过这两行代码:

h = quiver(...);
set(h,'MaxHeadSize',1e2,'AutoScaleFactor',1);

答案 4 :(得分:0)

从MATLAB文件交换中签出arrow3()

https://www.mathworks.com/matlabcentral/fileexchange/14056-arrow3

除了这些示例。

https://kr.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/14056/versions/16/previews/arrow3_examples.html

它比注释命令快,并且产生相似的结果。 使用以上示例

headWidth =0.8;  % 1/10 of annotation
headLength=0.8;  % 1/10 of annotation
LineLength = 0.08; % same as annotation


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

figure();
%hq = quiver(x,y,u,v);
p1 = [x(:) y(:)]; % data start point
u = u(:); v=v(:);
arrow3(p1,p1+LineLength*[u,v],'k',headWidth,headLength);

对不起,我无法发布这张密谋的图片,因为我需要赢得更多的声誉积分。箭头已关闭,并且所有大小均相似,如注释命令所给出的那样。