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

How to set different legends for MATLAB errorbar plot's dot and vertical line?

我有一个显示均值和标准差的 errorbar 图。我希望有一个圆圈的图例项、均值和一个 separate 的条形图。像,

--------------------------
| o mean                 |
| | standard deviation   |
--------------------------

MWE

errorbar([1 2], [2 3], [0.1 0.2], 'o');
legend('mean +- stddev', 'Location','north')

给我这个

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

errorbar([1 2], [2 3], [0.1 0.2], 'o');
hold on;
plot(1,3,'-b');
legend('Mean','Standard deviation','Location','north');

这是在 的基础上添加的:

  • 图例中表示误差线的线可以旋转,使其垂直,或保留其默认水平方向;
  • 那条线的末端是 "closed" 和 短线

方法比较通用,支持:

  • 任意 colorlinestylemarkers 作为 errorbar 的参数。请注意,实际条形图始终绘制为没有标记的实线(使用指定的颜色)。这是根据 errorbar 行为。
  • 新建的短线随图例移动
  • 那些行的宽度是一个可配置的参数。此外,errobar 的 length 是垂直情况下的参数。两者都根据图例以标准化单位定义。

该方法因 Matlab 版本而略有不同,因为 R2014b 中引入的图形 objects 发生了变化。此外,图例中的误差线可能是水平的或垂直的。 所以有四种情况

案例 I:R2014b 之前的版本,图例中的水平误差线

绘制数据和图例后,代码遵循以下步骤:

  1. 获取图例的children
  2. 在children中找到合适的行
  3. 得到它的xy坐标
  4. 利用这些坐标,在每一端创建两条短线。制作图例的那些线条 children,以便它们随之移动。

代码已在 Matlab R2010b 中测试。

%// 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 中,图例不再是轴 object,也没有 children。所以第 1 步和第 2 步必须针对案例 I 进行修改:

  1. 创建图例时获取图例图标
  2. 在这些图标中找到合适的行
  3. 获取它的 x 和 y 坐标
  4. 利用这些坐标,在每一端创建两条短线。使这些线与初始线共享 parent,以便它们与图例一起移动。

此外,此 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);

案例 III:R2014b 之前的版本,图例中的垂直误差线

这与情况 I 类似,但需要对线的 xy 坐标进行一些调整。此外,还引入了一个新参数来控制图例中误差条的长度。

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

案例 IV:R2014b,图例中的垂直误差线

同样,这与案例 II 类似,但对线条的坐标进行了一些调整。

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