![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1824802/f6123255dabfaff30e69cd89b1a283e7.png)
How do I place errorbars on my grouped bar graph using function ERRORBAR in MATLAB?
220 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
MathWorks Support Team
il 16 Dic 2011
Modificato: MathWorks Support Team
il 31 Gen 2025 alle 13:28
I use the following to create a grouped bar graph:
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];
bar(model_series, 'grouped');
hold on
errorbar( model_series,model_error)
The result is a grouped bar graph with error bars placed in between each group of bars:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1824801/f8a6155e1d827b4f96acd358698d724b.png)
I would like to have "errorbar" place error bars on the center of each bar.
Risposta accettata
MathWorks Support Team
il 17 Gen 2025
Modificato: MathWorks Support Team
il 31 Gen 2025 alle 13:28
The ability to specify that the "errorbar" function should display the error bars inside the patches is not available in MATLAB.
There are two workarounds for this limitation, usage of which depends on the release of MATLAB that you are using.
For R2019a or earlier releases:
Find the center of each bar and pass this data into "errorbar" with the respective error values.
% 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');
hold on % Find the number of groups and the number of bars in each group [ngroups, nbars] = size(model_series);
% 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 + (2i-1) * groupwidth / (2nbars); errorbar(x, model_series(:,i), model_error(:,i), 'k', 'linestyle', 'none'); end hold off
For R2019b or later releases:
Retrieve the x coordinate of each bar using the "XEndPoints" property and pass this data into "errorbar". To view the release-specific documentation on bar properties, execute the following command in the MATLAB R2019b command window:
>> web(fullfile(docroot, 'matlab/ref/matlab.graphics.chart.primitive.bar-properties.html'))
% 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');
hold on % Calculate the number of groups and number of bars in each group [ngroups,nbars] = size(model_series);
% Get the x coordinate of the bars x = nan(nbars, ngroups); for i = 1:nbars x(i,:) = b(i).XEndPoints; end
% Plot the errorbars errorbar(x',model_series,model_error,'k','linestyle','none'); hold off
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1824802/f6123255dabfaff30e69cd89b1a283e7.png)
Please follow the below link to search for the required information regarding the current release:
2 Commenti
Oleg Komarov
il 31 Gen 2017
TK1234 answered with "You are brilliant". I moved his answer to the comment section here.
Michael Abboud
il 1 Ago 2017
Hi Miraflor, this answer has been recently updated.
If the above method still results in uncentered error bars, I would suggest you look primarily at the 3rd-to-last line where the locations are calculated, or contact MathWorks Technical Support for further assistance.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Errorbars in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!