本文介绍了在 Matlab 中,如何更改箭袋图中的箭头样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改箭袋图中的默认箭头样式.我该如何更改?

I would like to change the default arrow head style in quiver plot. How can I change it?

推荐答案

For Matlab Version > R2014b

从 R2014b 版本开始,Matlab 修改了其图形组件的结构.这是使用 Matlab 的注释的最新代码.

Since R2014b version, Matlab has modified the structure of its graphical components. Here is the up-to-date code that uses Matlab's annotations.

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

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

Please note that this piece of code changes the head style and controls for the length of the line (in the left panel, you can see that arrows overlap on the upper-left part of the left subplot, while it does not on the right subplot). The length and width of the arrow heads are not modified.

对于这次编辑,我没有保留为角度编码的配色方案,并丢弃了动态头部大小.它使事情更清楚.

For this edit, I didn't keep the colors scheme that coded for the angle, and discarded the dynamic head size. It makes things clearer.

对于 Matlab 版本 <R2014b

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

Quiver plots are hard to modify. As @Luis Mendo said, you can modify the quiver function within the matlab install. However, you will still be limited by the complexity of programmatically drawing arrows with nice patches/lines. There might be an easier route using annotation - see the "Quiver - annotation" subplot that sets the headStyle property to cback1.

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

Annotations are graphical objects (lines, textboxes, arrows, ...) that you can be easily inserted by hand once a plot is done. They display additional text or point to a particular area for example. You can also insert them programmatically by defining their positions - and that's the option we will take. We first draw a regular quiver plot (left panel), get the blue lines' X and Y data, and use these coordinates to insert annotation arrows, each of them being displayed at the exact same location (same position, same angle, same size; right panel).

注释箭头有一些很好的属性,您可以轻松修改,例如 ColorHeadWidthHeadLengthHeadStyle.在下图中,我根据每个箭头相对于 x 轴的角度以及取决于长度的 headWidth 修改了每个箭头的颜色.

Annotation arrows have some nice properties you can easily modify, such as Color, HeadWidth, HeadLength, and HeadStyle. In the following plot, I modified each arrow's color depending on its angle against the x-axis, and headWidth that depends length.

下图

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

这篇关于在 Matlab 中,如何更改箭袋图中的箭头样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:30