bar with mean, std and significance star

2 views (last 30 days)
Gianluca Sesso
Gianluca Sesso on 2 Sep 2015
Answered: Samayochita on 25 Apr 2025
Hi! How can I represent a bar graph of mean values and standard deviations with the star of significance level of a t-test upon the bars? I tried with 'barweb' but it doesn't work on my matlab version and also it does not display any significance stars. Thank you in advance! Gianluca

Answers (1)

Samayochita
Samayochita on 25 Apr 2025
Hey Gianluca,
A bar graph can be created with error bars and significance stars in MATLAB using basic plotting functions like “bar”,errorbar”, and “text” for the stars.
Here is how it could be achieved:
  1. Compute Means and Standard Deviations.
means = [mean(data1), mean(data2)];
stds = [std(data1), std(data2)];
2. Plot bars for mean values. Adds error bars for standard deviations.
figure;
b = bar(means, 'FaceColor', [0.2 0.6 0.5]);
hold on;
errorbar(1:2, means, stds, '.k', 'LineWidth', 1.5);
3. Perform t-test to get p-values.
[h, p] = ttest2(data1, data2);
4. Add Significance Stars (*, **, ***, or 'n.s.') using text and plot” based on p-values.
if p < 0.001
stars = '***';
elseif p < 0.01
stars = '**';
elseif p < 0.05
stars = '*';
else
stars = 'n.s.';
end
% Add stars above the bars
maxY = max(means + stds) + 0.2;
text(1.5, maxY, stars, 'HorizontalAlignment', 'center', 'FontSize', 14);
For more information on the MATLAB functions used above, please refer to the following documentation links:

Categories

Find more on Convert Image Type in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!