Plot multiple histogram within 1 function
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
MadjeKoe
il 14 Ott 2020
Modificato: Adam Danz
il 15 Ott 2020
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 Commento
Risposta accettata
Sudhakar Shinde
il 15 Ott 2020
[~,ErrorLength]= size(out);
for i =1:ErrorLength
hold on
histogram(out(:,i),'normalization','pdf')
hold off
end
0 Commenti
Più risposte (2)
Steven Lord
il 14 Ott 2020
The "Plot Multiple Histograms" example on the documentation page shows how to superimpose two histogram plots using hold on.
0 Commenti
MadjeKoe
il 15 Ott 2020
9 Commenti
Adam Danz
il 15 Ott 2020
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
il 15 Ott 2020
Modificato: Adam Danz
il 15 Ott 2020
"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.
Vedere anche
Categorie
Scopri di più su Data Distribution Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!