File Exchange

image thumbnail

bihist (back to back histogram)

version (1.68 KB) by Mauro
create a back-to-back histogram


Updated 18 Sep 2009

View License

a good way to show differences between two sets of data about their location and distribution

Cite As

Mauro (2020). bihist (back to back histogram) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)


Thx, i hope you like the new version


%Just edited this one a bit to have more consistency in setting %colours and to keep the variable names a bit more clear %(left/right).
%Also, I put the scale back in and the histograms are now on the %same scale, although it shows a negative value on the left at %the moment.
%Hope it's a useful addition.

% BIHIST(X1,X2,N) open a new figure an plot a back to back histogram
% X1: variable 1 (right side, colour blue)
% X2: variable 2 (right side, colour green)
% n : number of classes for each histogramm

% make two dummy-histogram
[temp, d1] = hist(x1,n);
[temp, d2] = hist(x2,n);
clear temp
% calculate classes
d = min([d1 d2]):((max([d1 d2])-min([d1 d2]))/(n-1)) : max([d1 d2]);
% create the two histograms
templ = hist(x1,d);
tempr = hist(x2,d);
% scaling the histograms
%temp1 = temp1/sum(temp1);
%temp2 = temp2/sum(temp2)*-1;
% create bar plot
hold on
right = barh(tempr,'style','hist');
plot([0 0], [1 n],'k')
% change color of left histogram
set(left,'FaceColor',[0 0 1]);
% change color of right histogram
set(right,'FaceColor',[0 1 0]);
%clear the x-axis
xlim([-max(abs([templ tempr]))*dt max(abs([templ tempr]))*dt])
% mark the y-axis
yl = get(gca,'YTick');
b = (max(d)-min(d))/(n-1);
a = max(d)-b*n;
yl2 = yl*b+a;
ds = cell(size(yl2));
for k=1:length(yl2)
ds{k} = num2str(yl2(k),3);
clear k


new version is on the way


new colour and new label for x-axis

change in labeling the y-axis

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux