21

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

4

5 回答 5

24

对于 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');

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

对于这个编辑,我没有保留为角度编码的颜色方案,并丢弃了动态头部尺寸。它使事情更清楚。


对于 Matlab 版本 < R2014b

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

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

注释箭头有一些不错的属性,您可以轻松修改,例如ColorHeadWidthHeadLengthHeadStyle。在下图中,我根据每个箭头相对于 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');
于 2013-09-13T01:27:52.193 回答
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

改变 和 的值alpha达到beta预期的效果。

但是,这需要修改 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;):

    在此处输入图像描述

  • alpha = .5; beta = .5;

    在此处输入图像描述

于 2013-09-13T00:26:47.527 回答
3

你可以从这里开始:

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

然后您可以在此处查找 quiver 的可用属性:

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

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

编辑:此链接中的更多信息:箭头属性

在底部阅读,其中说

您可以选择一个注释,然后选择 Show M-code 以获得一个代码片段,您可以将其插入函数或脚本中以重现该注释。

于 2013-09-13T00:26:15.323 回答
2

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

h = quiver(...);
set(h,'MaxHeadSize',1e2,'AutoScaleFactor',1);
于 2014-11-18T09:01:44.807 回答
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);

抱歉,我无法发布此图的图片,因为我需要获得更多声望点。箭头是封闭的,大小都差不多,就像注释命令给出的那样。

于 2020-08-29T19:00:14.483 回答