creating standard error of mean for bar graphs in code, can you please correct/modify/help my code?

5 views (last 30 days)
in my code i graphed my data and i simply want to show the standard error of mean for each bar in each bar plot. Heres what i have but I am not confident that it works can someone please confirm?
% Compute the SEM of the data for each bar in the Size plot
n = numel(averageSize);
SEM_avgSize = std(avgSize) / sqrt(n);
SEM_values = repmat(SEM_avgSize, n, 1);
% Plot the data with error bars
subplot(3, 4, 7);
bar(avgSize, 'LineWidth', 2);
hold on;
errorbar(1:n, avgSize, SEM_values, 'k.', 'LineWidth', 1.5);
hold off;
title('Average Size of Organelles', 'FontSize', fontSize);
xlabel('Zone', 'FontSize', fontSize);
ylabel('Size (\mum^2)', 'FontSize', fontSize);
set(gca, 'FontSize', fontSize);

Accepted Answer

Jeff Miller
Jeff Miller on 28 Apr 2023
Hard to say for sure without knowing what the original data were, but that doesn't look right to me. Can you explain how the values of avgSize were computed? Most likely, there should be a different value of SEM for each bar, computed using only the n and std of the scores that were averaged to get that bar's avgSize.
  3 Comments

Sign in to comment.

More Answers (1)

LeoAiE
LeoAiE on 28 Apr 2023
Your code seems to be mostly correct, but there are a few things that need to be adjusted to compute and display the standard error of the mean (SEM) correctly for each bar in the bar plot.
  1. The variable names in your code are inconsistent. You mentioned averageSize and avgSize. Make sure to use the same variable name throughout your code.
  2. To calculate the SEM for each bar, you need to compute the standard deviation and SEM for each group (zone) separately.
Assuming averageSize is a matrix where each column represents a different zone, you can modify your code as follows:
% Compute the SEM of the data for each bar in the Size plot
n = size(averageSize, 1);
std_avgSize = std(averageSize, 0, 1); % Standard deviation for each zone
SEM_avgSize = std_avgSize / sqrt(n); % SEM for each zone
% Plot the data with error bars
avgSize_mean = mean(averageSize, 1); % Mean for each zone
num_zones = size(averageSize, 2);
subplot(3, 4, 7);
bar(avgSize_mean, 'LineWidth', 2);
hold on;
errorbar(1:num_zones, avgSize_mean, SEM_avgSize, 'k.', 'LineWidth', 1.5);
hold off;
title('Average Size of Organelles', 'FontSize', fontSize);
xlabel('Zone', 'FontSize', fontSize);
ylabel('Size (\mum^2)', 'FontSize', fontSize);
set(gca, 'FontSize', fontSize);
  1 Comment
kevin
kevin on 29 Apr 2023
how about this:
% Compute the SEM of the data for each bar in the avgSize plot
n = numel(avgSize);
SEM_avgSize = std(avgSize) / sqrt(n);
SEM_valuessize = repmat(SEM_avgSize, n, 1);
% Plot the data with error bars
subplot(3, 4, 7);
bar(avgSize, 'LineWidth', 2);
hold on;
errorbar(1:n, avgSize, SEM_valuessize, 'k.', 'LineWidth', 1.5);
hold off;
title('Average Size of Organelles', 'FontSize', fontSize);
xlabel('Zone', 'FontSize', fontSize);
ylabel('Size (\mum^2)', 'FontSize', fontSize);
set(gca, 'FontSize', fontSize);

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!