如何为MATLAB errorbar plot设置不同的图例';s点和垂直线?

如何为MATLAB errorbar plot设置不同的图例';s点和垂直线?,matlab,matlab-figure,Matlab,Matlab Figure,我有一个显示平均值和标准偏差的errorbar图。我希望圆圈有一个图例项,平均值,条形图有一个单独的图例项。大概 -------------------------- | o mean | | | standard deviation | -------------------------- MWE 给我这个 一种方法是添加一条不可见的线。试试这个: errorbar([1 2], [2 3], [0.1 0.2], 'o'); hold on; plot(

我有一个显示平均值和标准偏差的
errorbar
图。我希望圆圈有一个图例项,平均值,条形图有一个单独的图例项。大概

--------------------------
| o mean                 |
| | standard deviation   |
--------------------------
MWE 给我这个


一种方法是添加一条不可见的线。试试这个:

errorbar([1 2], [2 3], [0.1 0.2], 'o');
hold on;
plot(1,3,'-b');
legend('Mean','Standard deviation','Location','north');
这将建立在它的基础上,并向其中添加以下内容:

  • 图例中代表错误条的线可以旋转,使其垂直,或者保持默认的水平方向
  • 该行的两端用短线“闭合”
该方法非常通用,因为它支持:

  • 任意颜色线型标记作为
    错误栏
    的参数。请注意,实际条形图始终绘制为没有标记的实线(使用指定的颜色)。这符合
    errorbar
    行为
  • 新创建的短线与图例一起移动
  • 这些行的宽度是可配置的参数。此外,errobar的长度是垂直情况下的一个参数。两者都是以图例的规格化单位定义的
由于R2014b中引入的图形对象发生了变化,因此该方法因Matlab版本而异。此外,图例中的线可以是水平的,也可以是垂直的。 因此有四个病例

案例一:R2014b之前,图例中的水平误差条 绘制数据和图例后,代码将执行以下步骤:

  • 得到传奇的孩子
  • 在这些孩子中找到合适的路线
  • 得到它的x和y坐标
  • 利用这些坐标,在每一端创建两条短线。将这些线条作为传奇的子元素,以便它们跟随传奇移动
  • 该代码已经在MatlabR2010B中进行了测试

    %// Define graph aspect
    color_spec = 'r';       %// color of data and bars.
    linestyle_spec = '--';  %// linestyle for data. The bars are always solid
    marker_spec = 'o';      %// marker for data
    wid = .12;              %// width of bar in legend. Normalized units
    
    %// Plot errobar
    h_er = errorbar([1 2], [2 3], [0.1 0.2], [color_spec linestyle_spec marker_spec]); 
    
    %// Add dummy line for legend (as per Jens Boldsen) and create legend
    hold on;
    plot(0,0,color_spec); %// linestyle is always solid with no marker
    h_le = legend('Mean','Standard deviation','Location','north');
    
    %// Add short lines at each end of line in the legend
    c_le = get(h_le, 'Children');                                                %// step 1
    h_li = findobj(c_le, 'linestyle','-', 'color',color_spec, 'marker','none');  %// step 2
    h_li = h_li(1); %// in case there's more than one
    li_x = get(h_li,'XData');                                                    %// step 3
    li_y = get(h_li,'YData');
    line(li_x([1 1]), li_y+wid*[-.5 .5], 'parent',h_le, 'color',color_spec);     %// step 4
    line(li_x([2 2]), li_y+wid*[-.5 .5], 'parent',h_le, 'color',color_spec);
    

    案例二:R2014b,图例中的水平误差条 在Matlab R2014b中,图例不再是轴对象,并且没有子对象。因此,第1步和第2步必须根据情况I进行修改:

  • 创建图例时获取图例的图标
  • 在这些图标中找到相应的行
  • 得到它的x和y坐标
  • 利用这些坐标,在每一端创建两条短线。使这些线与初始线共享父线,以便它们与图例一起移动
  • 此外,此Matlab版本中的新语法有助于稍微简化代码

    %// Define graph aspect
    color_spec = 'r';       %// color of data and bars.
    linestyle_spec = '--';  %// linestyle for data. The bars are always solid
    marker_spec = 'o';      %// marker for data
    wid = .12;              %// width of bar in legend. Normalized units
    
    %// Plot errobar
    h_er = errorbar([1 2], [2 3], [0.1 0.2], [color_spec linestyle_spec marker_spec]); 
    
    %// Add dummy line for legend (as per Jens Boldsen)
    hold on;
    plot(0,0,color_spec); %// linestyle is always solid with no marker
    
    %// Create legend and add short lines at each end of line in the legend
    [~, icons] = legend('Mean','Standard deviation','Location','north');           %// 1
    li = findobj(icons, 'type','line', 'linestyle','-');                           %// 2
    li = li(1); %// in case there's more than one
    li_x = li.XData;                                                               %// 3
    li_y = li.YData;
    line(li_x([1 1]), li_y+wid*[-.5 .5], 'parent',li.Parent, 'color',color_spec);  %// 4
    line(li_x([2 2]), li_y+wid*[-.5 .5], 'parent',li.Parent, 'color',color_spec);
    

    案例三:R2014b之前,图例中的垂直误差条 这与案例一类似,但需要对线的x和y坐标进行一些调整。此外,还引入了一个新参数来控制图例中errorbar的长度

    %// Define graph aspect
    color_spec = 'r';       %// color of data and bars.
    linestyle_spec = '--';  %// linestyle for data. The bars are always solid
    marker_spec = 'o';      %// marker for data
    wid = .04;              %// width of bar in legend. Normalized units
    len = .45;              %// length of main bar in legend. Normalized units
    
    %// Plot errobar
    h_er = errorbar([1 2], [2 3], [0.1 0.2], [color_spec linestyle_spec marker_spec]); 
    
    %// Add dummy line for legend (as per Jens Boldsen) and create legend
    hold on;
    plot(0,0,color_spec); %// linestyle is always solid with no marker
    h_le = legend('Mean','Standard deviation','Location','north');
    
    %// Add short lines at each end of line in the legend
    c_le = get(h_le, 'Children');    
    h_li = findobj(c_le, 'linestyle','-', 'color',color_spec, 'marker','none');
    h_li = h_li(1); %// in case there's more than one
    set(h_li,'XData', repmat(mean(get(h_li,'XData')),1,2));
    set(h_li,'YData', mean(get(h_li,'YData'))+len*[-.5 .5]);
    li_x = get(h_li,'XData');
    li_y = get(h_li,'YData');
    line(li_x+wid*[-.5 .5], li_y([1 1]), 'parent',h_le, 'color',color_spec);
    line(li_x+wid*[-.5 .5], li_y([2 2]), 'parent',h_le, 'color',color_spec);
    

    案例四:R2014b,图例中的垂直误差条 同样,这与第二种情况类似,但对线的坐标进行了一些调整

    %// Define graph aspect
    color_spec = 'r';       %// color of data and bars.
    linestyle_spec = '--';  %// linestyle for data. The bars are always solid
    marker_spec = 'o';      %// marker for data
    wid = .05;              %// width of small bars in legend. Normalized units
    len = .45;              %// length of main bar in legend. Normalized units
    
    %// Plot errobar
    h_er = errorbar([1 2], [2 3], [0.1 0.2], [color_spec linestyle_spec marker_spec]); 
    
    %// Add dummy line for legend (as per Jens Boldsen)
    hold on;
    plot(0,0,color_spec); %// linestyle is always solid with no marker
    
    %// Create legend, modify line and add short lines
    [~, icons] = legend('Mean','Standard deviation','Location','north');             
    li = findobj(icons, 'type','line', 'linestyle','-');
    li = li(1); %// in case there's more than one
    li.XData = repmat(mean(li.XData),1,2);                                           
    li.YData = mean(li.YData)+len*[-.5 .5];
    li_x = li.XData;
    li_y = li.YData;
    line(li_x+wid*[-.5 .5], li_y([1 1]), 'parent',li.Parent, 'color',color_spec);
    line(li_x+wid*[-.5 .5], li_y([2 2]), 'parent',li.Parent, 'color',color_spec);
    

    这太聪明了,伙计。谢谢<代码>绘图(NaN,'-b')更安全一些,我想,因为您严格确保它不会干扰当前绘图。@CrazyRat:
    绘图(NaN,'-b')
    并不安全,因为它绘制的是点(1,NaN),这可能会导致x轴发生变化,遗憾的是
    绘图(NaN,NaN,'-b')
    也不起作用。为了更安全,您只需确保在轴设置的限制范围内绘制一个点,这可以通过以下方式完成:
    ax=axis;图(平均值(ax(1:2)),平均值(ax(3:4)),“-b”)包括MWE做得很好!这应该是正常的,但是…这很好,但是我在尝试设置图例框的解释器时遇到了一个问题。我做了
    [h,icons]=图例('Mean','$\pm$标准错误','Location','northeast');集合(h,‘解释器’、‘乳胶’)但解释器仍然是tex,而不是latex。哦,我忘了提到,但我是R2014b的案例。今晚我会检查一下(意思是大约12小时后),我现在没有R2014b,我已经能够测试它了。解释器属性设置为
    latex
    ,但是
    $
    符号的解释不正确。你不能用
    [h,icons]=图例('Mean','\pm标准错误','Location','northeast')<使用默认的
    'text'
    解释器正确解释code>pm(不带
    $
    %// Define graph aspect
    color_spec = 'r';       %// color of data and bars.
    linestyle_spec = '--';  %// linestyle for data. The bars are always solid
    marker_spec = 'o';      %// marker for data
    wid = .05;              %// width of small bars in legend. Normalized units
    len = .45;              %// length of main bar in legend. Normalized units
    
    %// Plot errobar
    h_er = errorbar([1 2], [2 3], [0.1 0.2], [color_spec linestyle_spec marker_spec]); 
    
    %// Add dummy line for legend (as per Jens Boldsen)
    hold on;
    plot(0,0,color_spec); %// linestyle is always solid with no marker
    
    %// Create legend, modify line and add short lines
    [~, icons] = legend('Mean','Standard deviation','Location','north');             
    li = findobj(icons, 'type','line', 'linestyle','-');
    li = li(1); %// in case there's more than one
    li.XData = repmat(mean(li.XData),1,2);                                           
    li.YData = mean(li.YData)+len*[-.5 .5];
    li_x = li.XData;
    li_y = li.YData;
    line(li_x+wid*[-.5 .5], li_y([1 1]), 'parent',li.Parent, 'color',color_spec);
    line(li_x+wid*[-.5 .5], li_y([2 2]), 'parent',li.Parent, 'color',color_spec);