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

5 次查看(过去 30 天)
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);

采纳的回答

Jeff Miller
Jeff Miller 2023-4-28
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 个评论

请先登录,再进行评论。

更多回答(1 个)

LeoAiE
LeoAiE 2023-4-28
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 个评论
kevin
kevin 2023-4-29
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);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by