File Exchange

## bihist (back to back histogram)

version 1.3.0.0 (1.68 KB) by Mauro

### Mauro (view profile)

create a back-to-back histogram

Updated 18 Sep 2009

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) (https://www.mathworks.com/matlabcentral/fileexchange/23312-bihist-back-to-back-histogram), MATLAB Central File Exchange. Retrieved .

Mauro

### Mauro (view profile)

Thx, i hope you like the new version

Anastasia

### Anastasia (view profile)

%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.

% 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;
templ=templ*-1
% create bar plot
left=barh(templ,'style','hist')
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
%set(gca,'XTick',0)
%set(gca,'XTickLabel',{''})
dt=1.2;
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);
end
set(gca,'YTickLabel',ds)
clear k

Mauro

### Mauro (view profile)

new version is on the way