gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:50:33 25 4
gpt4 key购买 nike

我有一个显示均值和标准差的errorbar 图。我希望有一个圆圈的图例项、均值和一个单独的 条形图项。类似的东西,

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

MWE

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

给我这个

enter image description here

最佳答案

这建立在 Jens Boldsen's answer 之上,它添加了以下内容:

  • 图例中代表误差条的线可以旋转以使其垂直,或保留其默认的水平方向;
  • 该行的末端用短线“封闭”。

该方法非常通用,因为它支持:

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

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

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

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

  1. 获取传奇的 child
  2. 在这些 child 中找到合适的行
  3. 获取它的xy坐标
  4. 利用这些坐标,在每一端创建两条短线。让这些线条成为传说的 child ,以便它们随之移动。

代码已在 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);

enter image description here

案例二:R2014b,图例中的水平误差线

在 Matlab R2014b 中,图例不再是坐标区对象,并且没有子对象。因此,针对案例 I,必须修改步骤 1 和 2:

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

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

enter image description here

案例 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);

enter image description here

案例四: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);

enter image description here

关于matlab - 如何为 MATLAB errorbar plot 的点和垂直线设置不同的图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28041629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com