matlab - 如何在分组栏中绘制误差线?

标签 matlab bar-chart errorbar

我想在分组条中绘制误差条。我写了下面的代码

x = categorical({'a', 'b', 'c', 'd', 'e'});
y = [6816,7215; 5824,6180; 4860,5200; 3860,4206; 2838,3185];
errlow = [238,337;270,355;303,297;291,340;259,382];
errhigh = [231,264;225,337;153,171;185,286;167,247];

b = bar(x,y);

hold on

xtips1 = b(1).XEndPoints;
ytips1 = b(1).YEndPoints;

er = errorbar(xtips1, ytips1, errlow(:,1), errhigh(:,1));    
er.Color = [0 0 0];                            
er.LineStyle = 'none';

hold on

xtips2 = b(2).XEndPoints;
ytips2 = b(2).YEndPoints;

er2 = errorbar(xtips2, ytips2, errlow(:,2), errhigh(:,2));    
er2.Color = [0 0 0];                            
er2.LineStyle = 'none';

hold off

ylim([2400 7600]);

错误栏显示在图表中。请看一下,它们直接位于 a、b、c、d 和 e 上方,但不在条形图上,如图所示。

enter image description here

我希望它们位于关联的栏上(“绿色”标记的错误应位于右侧栏,而“红色”标记的错误应位于左侧栏),如图所示。我该怎么做?

提前致谢!

最佳答案

这是 MathWorks Support Team 提供的答案,如发布于MATLAB Answers (一些小的修改除外)。


The ability to specify that the errorbar function should display the error bars inside the patches is not available in MATLAB. There are two work arounds for this limitation, usage of which depends on the release of MATLAB that you are using.

  1. If you are using R2019a or earlier releases, find the center of each bar and pass this data into errorbar with the respective error values.
  2. If you are using R2019b or later releases, retrieve the x coordinate of each bar using the XEndPoints property and pass this data into errorbar.

The following is an example of the above:

% Example data
model_series = [10 40 50 60; 20 50 60 70; 30 60 80 90];
model_error = [1 4 8 6; 2 5 9 12; 3 6 10 13];
b = bar(model_series, 'grouped');

对于 MATLAB R2019a 或更早版本:

hold on
% Find the number of groups and the number of bars in each group
ngroups = size(model_series, 1);
nbars = size(model_series, 2);
% Calculate the width for each bar group
groupwidth = min(0.8, nbars/(nbars + 1.5));
% Set the position of each error bar in the centre of the main bar
% Based on barweb.m by Bolu Ajiboye from MATLAB File Exchange
for i = 1:nbars
    % Calculate center of each bar
    x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);
    errorbar(x, model_series(:,i), model_error(:,i), 'k', 'linestyle', 'none');
end
hold off

对于 MATLAB 2019b 或更高版本:

hold on
% Calculate the number of bars in each group
nbars = size(model_series, 2);
% Get the x coordinate of the bars
x = [];
for i = 1:nbars
    x = [x ; b(i).XEndPoints];
end
% Plot the errorbars
errorbar(x',model_series,model_error,'k','linestyle','none')'
hold off

enter image description here

关于matlab - 如何在分组栏中绘制误差线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65734026/

相关文章:

matlab - 如何在 Matlab 中使用经过训练的神经网络在真实系统中进行分类

javascript - 我可以使用 C3.js 制作一个堆叠条形图,其中每个列都有单独的数据值吗?

python - Matplotlib 非对称 yerr 方式偏离

python - 如何使用 matplotlib.errorbar 更改单个标记的颜色?

MATLAB 2014a (8.3) 编译器运行时错误 libmwlaunchermain.so

matlab - 裁剪图像的中心: Matlab

reporting-services - 在 SSRS 中,当数据标签字体出现在条形图上时,如何有条件地更改图表上数据标签字体的颜色

python - 如何在误差线旁边添加误差值?

matlab - 在Matlab上用于3D数据的椭圆拟合

javascript - Shield UI 条形图重叠