histogram with two sets of data

Hello, I want to make an histogram exactly like the image here. Have you an idea ?

 Accepted Answer

Star Strider
Star Strider on 6 May 2015
Use the bar function with the 'grouped' option. The error bars and the comparison lines between the plotted bars are a bit more difficult, and the code the plot them depends on whether you are using R2014a or earlier, or R2014b or later.

4 Comments

Ok thank you, I use R2010a.
I apologise for the delay. I had to launch R2014a to be sure the code works. You will probably need to experiment with it to get the exact result you want.
This code:
data = randi([870 1020], 4, 2); % Create Data
errs = randi([12 15], 4, 2 ); % Create Errors
cmap = gray(4);
xval = 1:4;
figure(1)
hBar = bar(xval,data); % Plot Data, Get Handle
set(hBar(1), 'FaceColor', cmap(2,:)) % Colour First Bar Set
set(hBar(2), 'FaceColor', cmap(3,:)) % Colour First Bar Set
set(gca, 'YLim', [870 1080]) % Set Y-Axis Limits
hold on
for k1 = 1:length(hBar) % Loop: Plots Error Bars
hb = get(get(hBar(k1),'Children'), 'XData');
midbar = mean(hb);
errorbar(midbar, data(:,k1), errs(:,k1), '.') % plotting errors
sigbarx(k1,:) = midbar; % Use To Plot Significance Bars
end
plot(sigbarx(:,1), [1 1]*1065, '-k', 'LineWidth', 2)
plot(sigbarx(1,1:2), [1 1]*1075, '-k', 'LineWidth', 2)
ylabel('temps de reaction (ms)')
legend('valide','non valide', 'Location', 'NE')
xtklbl = {'indice enotionnel', 'indice neutre', 'indice enotionnel', 'indice neutre'};
set(gca, 'XTickLabel',xtklbl)
text(1.5, 855, 'question enotionnelle', 'HorizontalAlignment','center')
text(3.5, 855, 'question semantique', 'HorizontalAlignment','center')
hold off
produces this plot (that is as close as I can get to the plot you posted):
Yes! It's perfect! Thank you very much.
My pleasure!

Sign in to comment.

More Answers (1)

Elvira Ruiz
Elvira Ruiz on 23 Oct 2016
I do not have enough words to express my gratitude. THANK YOU!

4 Comments

My pleasure!
Hi, I'm having a small problem with the errorbar and I was wondering if you might know why. I was trying out your code, without any modifications just to see how it works, and when I try to plot the errorbars a error message pops up saying X,Y and errorbars have to be the same length. Trying to find out the problem, I see that the line "get(hBar(k1),'Children')" returns an empty array every time. I am using MATLAB R2016a, could it maybe be that some functions have changed in this version? Do you have any idea of how to fix this?
Thanks again!
My pleasure.
That code was written for R2014a. HG2 graphics were introduced in R2014b.
Example code for R2014b (and later versions) for bar chart error bars is:
y = [212206000000 229040000000 39312320000; 119783500000 169247500000 128418300000 ; 211838000000 706581300000 85349300000];
hBar = bar(y);
set(gca,'XTickLabel', {'300 ','350 ','400 '})
legend('C','S','T', 'Location','N')
grid on
ylabel('N/m^{2}')
xlabel ('\mum')
colormap summer
for k1 = 1:size(hBar,2)
ctr(k1,:) = bsxfun(@plus, hBar(k1).XData, [hBar(k1).XOffset]'); % Centres Of Bar Groups
ydt(k1,:) = hBar(k1).YData; % Y-Data Of Bar Groups
end
hold on
for k1 = 1:size(hBar,2)
errorbar(ctr(k1,:), ydt(k1,:), ones(size(ydt(k1,:)))*0.1.*ydt(k1,:), '.r') % Plot 10% Error Bars
end
hold off
It’s been a while since I ran that, so I ran it just now on R2016b to check it. It works.
This also gives me the opportunity to include the R2014b code here. That should help other people who are looking for both versions.
Super helpful! Thank you for taking the time to post this answer!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!