Adding sigstar to grouped bar graph
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1741041/image.png)
0 个评论
回答(2 个)
0 个评论
Hi @Hanna Armstrong ,
I do completely understand the reason of not sharing humongous amount of data . However, I have generated generic sample data to demonstrate the process. You can replace this with your actual data later.
% Sample data for three groups (blue, red, yellow) and four participants
data = [10 15 20; 12 18 22; 8 14 18; 11 16 21]; % Define the data matrix with values for each group and participant
Next, I plot the grouped bar graph using the bar function.
bar(data, 'grouped'); % Create a grouped bar graph using the data matrix legend('Blue', 'Red', 'Yellow'); % Add a legend to the graph for each group xlabel('Participants'); % Set the label for the x-axis ylabel('Values'); % Set the label for the y-axis title('Grouped Bar Graph'); % Set the title of the graph hold on; % Hold the current graph to add more elements
To add sigmas between the bars of different groups, I used the errorbar function which allows you to display error bars representing the variability or uncertainty in the data.
numGroups = size(data, 2); % Calculate the number of groups from the data matrix
numBars = size(data, 1); % Calculate the number of bars based on the data matrix
groupWidth = min(0.8, numBars/(numBars + 1.5)); % Calculate the width of each group in the bar graph
for i = 1:numBars
x = (1:numGroups) - groupWidth/2 + (2*i-1) * groupWidth / (2*numBars); % Calculate the x positions for each group
errorbar(x, data(i,:), zeros(1,numGroups), 'k', 'linestyle', 'none'); % Add error bars to the graph for each group
end
hold off; % Release the current graph to end the plotting
Please see attached plot.
![](/matlabcentral/answers/uploaded_files/1748499/IMG_7355.jpeg)
Hope this helps. Please let me know if you have any further questions.
0 个评论
另请参阅
类别
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!