Clear Filters
Clear Filters

How to plot 2 histograms on the same axis?

9 views (last 30 days)
I need to plot two histograms using the same axis. Code is below:
X1 = GalList.dist;
Y1 = GalList.mass;
XY = [X1,Y1];
XY2=XY(~any(isnan(XY),2),:); % Remove NaN
XY2=XY2';
[Y,I]=sort(XY2(1,:)); % Sort by distance, I are indices
XY3 = XY2(:,I);
X2 = XY3(1,:);X2=X2';
Y2 = XY(2,:);Y2=Y2';
Y3 = cumsum(Y1); %same function as Y4
A = sort(X2);
B = sort(Y2,'descend'); %flips plot (for newgalaxy, need to use ascend for plot flip)
Y4 = cumsum(B); %total blue lum within 100 Mpc of total catalog
for s=1:length(X2)
if X2(s)<=20
p(s)=1;
else
p(s)=0;
end
q(s)=p(s).*X2(s);
w(s)=p(s).*Y1(s);
r(s)=p(s).*Y3(s);
end
w(w==0)=[];
w(find(isnan(w)))=[];
q(q==0)=[];
q(find(isnan(q)))=[];
r(r==0)=[];
r(find(isnan(r)))=[];
Q = cumsum(w);
L = 2.6678e+03;
figure(1);
D = cumsum(w/L);
D(D==0)=[];
D(find(isnan(D)))=[];
R = sum(D(:));
hist(D,50)
for s=1:length(X2)
if X2(s)<=20
p(s)=1;
else
p(s)=0;
end
o(s)=p(s).*X2(s);
v(s)=p(s).*Y1(s);
u(s)=p(s).*Y3(s);
o(o==0)=[];
o(find(isnan(o)))=[];
v(v==0)=[];
v(find(isnan(v)))=[];
u(u==0)=[];
u(find(isnan(u)))=[];
Q2 = cumsum(o);
U = 2.6678e+03;
figure(1);
D2 = cumsum(o/U);
D2(D2==0)=[];
D2(find(isnan(D2)))=[];
R2 = sum(D2(:));
hist(D2,50)
I was trying this but it doesn't work:
[n1, xout1] = hist(D,100);
bar(xout1,n1,'r'); grid; hold
[n2, xout2] = hist(D2,100);
bar(xout2,n2,'g');

Answers (1)

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh on 30 Jun 2015
Hi,
Create a figure before using hist, create an axes in the figure and get its handle. Use hold command to allow multiple graphs on same axis. Then pass the axes handle to the hist() command.
figure
h = axes();
hold(h,'on')
hist(h,D2,100)
...
I think this would work.
Good Luck!

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!