如何为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的长度是垂直情况下的一个参数。两者都是以图例的规格化单位定义的李>
%// 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进行修改:
%// 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);