如何在分组条中绘制错误条?
How to plot errorbars in a grouped bar?
我想在我的分组条中绘制错误条。我写了下面的代码
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的正上方,而不是如图所示的横条上。
我希望它们出现在相关栏上('GREEN' 标记的错误应在右侧栏,而 'RED' 标记的错误应在左侧栏),如图所示。我该怎么做?
提前致谢!
这是MathWorks Support Team, as posted on 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.
- 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.
- 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
我想在我的分组条中绘制错误条。我写了下面的代码
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的正上方,而不是如图所示的横条上。
我希望它们出现在相关栏上('GREEN' 标记的错误应在右侧栏,而 'RED' 标记的错误应在左侧栏),如图所示。我该怎么做?
提前致谢!
这是MathWorks Support Team, as posted on 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.
- 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.- If you are using R2019b or later releases, retrieve the x coordinate of each bar using the
XEndPoints
property and pass this data intoerrorbar
.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