Plot multiple histogram within 1 function

Hi all! I made some histograms, but now I'm trying to make a function that plots multiple histograms at the same time. I want 1 plot with 4 overlapping histograms of different colors and a legend. Can someone please explain to me how to do this? I tried to make a loop (see bottom) but it doesn't seem to work.
histogram_color = [0 .7 .7];
histogram(error1,'normalization','pdf','edgecolor',histogram_color,'facecolor',histogram_color,'facealpha',.5)
x = -90:1:90;
f1 = fitdist(error1,'kernel');
pdf1 = pdf(f1,x);
l1 = line(x,pdf1,'linestyle','-','color',histogram_color,'linewidth',3);
histogram_color = [0 0 .7];
histogram(error2,'normalization','pdf','edgecolor',histogram_color,'facecolor',histogram_color,'facealpha',.5)
x = -90:1:90;
f2 = fitdist(error2,'kernel');
pdf2 = pdf(f2,x);
l2 = line(x,pdf2,'linestyle','-','color',histogram_color,'linewidth',3);
x = -90:1:90;
for i = 1:length(out)
histogram(out(i),'normalization','pdf','edgecolor',histogram_color,'facecolor',histogram_color,'facealpha',.5);
f(i) = fitdist(out(i),'kernel');
pdf(i) = pdf(f(i),x);
l(i) = line(x,pdf(i),'linestyle','-','color',histogram_color,'linewidth',3);
end

댓글 수: 1

Adam Danz
Adam Danz 2020년 10월 14일
You're missing "hold on"
Otherwise the 2nd histogram replaces the first.

댓글을 달려면 로그인하십시오.

 채택된 답변

Sudhakar Shinde
Sudhakar Shinde 2020년 10월 15일

0 개 추천

[~,ErrorLength]= size(out);
for i =1:ErrorLength
hold on
histogram(out(:,i),'normalization','pdf')
hold off
end

추가 답변 (2개)

Steven Lord
Steven Lord 2020년 10월 14일

1 개 추천

The "Plot Multiple Histograms" example on the documentation page shows how to superimpose two histogram plots using hold on.
MadjeKoe
MadjeKoe 2020년 10월 15일

0 개 추천

S = load('/Users/mkoenraad/Desktop/Scriptie/exercise_2/simulated_serial_bias_dataset.mat');
res = S.res;
targets = res(:,7);
responses = res(:,8);
orx = [3,4,5,6];
tgt = [1,2,3,4];
num = numel(orx);
out = cell(1,num);
for k = 1:num
idx = targets==tgt(k);
resp = responses(idx);
ori = res(idx,orx(k));
err = resp - ori;
err(err<-90) = err(err<-90)+180;
err(err>90) = err(err>90)-180;
out{k} = err;
end
out = [out{:}];
ero1 = out(:,1);
ero2 = out(:,2);
ero3 = out(:,3);
ero4 = out(:,4);

댓글 수: 9

MadjeKoe
MadjeKoe 2020년 10월 15일
Is this ok? I don't know how to only add my 'out' variable
histogram_color = [0 .7 .7];
x = -90:1:90;
[~,ErrorLength]= size(out);
for i =1:ErrorLength
figure(i)
hold on
histogram(out(:,i),'normalization','pdf','edgecolor',histogram_color,'facecolor',histogram_color,'facealpha',.5)'
f = fitdist(out(:,i),'kernel');
pdf = pdf(f,x);
l = line(x,pdf,'linestyle','-','color',histogram_color,'linewidth',3);
hold off
end
yes. ok. Try this code at bottom. it works fine.
MadjeKoe
MadjeKoe 2020년 10월 15일
I keep on getting this message:
Unable to use a value of type prob.KernelDistribution as an index.
Error in forumans (line 35)
pdf = pdf(f,x);
I don't understand why because it worked before
Try this code:
[~,ErrorLength]= size(out);
for i =1:ErrorLength
hold on
histogram(out(:,i),'normalization','pdf')
hold off
end
This gives output on single graph with different histograms without using kernel. As i dont have toolbox i can not check for
f = fitdist(out(:,i),'kernel');
pdf = pdf(f,x);
MadjeKoe
MadjeKoe 2020년 10월 15일
Thank you so much!!!! This is exactly want I meant!
Glad to help you.
You only need to "hold on" once and that was mentioned in a comment under the question 13 hours prior to this sub-thread.
hold on % <--- Move here
[~,ErrorLength]= size(out);
for i =1:ErrorLength
% hold on <--- Remove
histogram(out(:,i),'normalization','pdf')
% hold off <--- remove or move out of the loop
end
Adam Danz
Adam Danz 2020년 10월 15일
편집: Adam Danz 2020년 10월 15일
"I did not work."
I doubt it. It's the same solution as Sudhakar Shinde's and you indicated that it worked. There must have been some other problem (which is likely given the new propblem follwing "clear all").
" I tried 'clear all' & now my whole thing is not working anymore"
'clear all' is rarely necessary. You likely were using a global variable named x and now it's no longer defined. If that's the case, it s a good lesson to never use global variables. Now you need to figure out where x was defined.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

질문:

2020년 10월 14일

편집:

2020년 10월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by