【问题标题】:In Matlab how do I change the arrow head style in quiver plot?在 Matlab 中,如何更改箭袋图中的箭头样式?
【发布时间】:2013-09-17 13:01:25
【问题描述】:

我想更改箭袋图中的默认箭头样式。怎么改?

【问题讨论】:

    标签: matlab vector plot


    【解决方案1】:

    你可以从这里开始:

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

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

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

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

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

    阅读底部,哪里说

    您可以选择一个注释,然后选择 Show M-code 以获得一个 您可以在函数或脚本中插入以重现的代码 sn-p 注释。

    【讨论】:

    • 我找到了,但我需要的是头部样式,而不是尺寸。我找不到 HeadStyle 的任何属性。
    • 看看这个链接:Arrow properties。阅读底部,You can select an annotation and then choose Show M-code to obtain a code snippet that you can insert in a function or script to reproduce the annotation.
    【解决方案2】:

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

    %// 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;):

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

    【讨论】:

    • 你究竟是如何使用这些参数的?
    • 我使用 which 命令找出了 quiver 的 m 文件,并更改了这些参数,但这没有任何效果。我正在使用 r2012b。
    • @qed 你说得对,它们不起作用。看来实际使用的alphabeta...\MATLAB\R2010b\toolbox\matlab\specgraph\@specgraph\@quivergroup\@specgraph\@quivergroup\refresh.m
    【解决方案3】:

    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版本

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

    Annotations 是图形对象(线条、文本框、箭头等),一旦绘制完成,您可以轻松地手动插入它们。例如,它们显示附加文本或指向特定区域。您还可以通过定义它们的位置以编程方式插入它们 - 这就是我们将采用的选项。我们首先绘制一个常规的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');
    

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • @Rufflewind 我不同意你的观点。这个答案虽然很短,但不是评论。
      【解决方案5】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-26
        • 1970-01-01
        • 2017-02-22
        • 2011-03-01
        • 2016-07-04
        • 2022-01-18
        • 1970-01-01
        • 2017-03-17
        相关资源
        最近更新 更多