Histograms - Mean calculation - Comparing more pdf

Hi everyone!
I have 3 questions!
1) I would like to do an histogram in which i have numbers in ordinates and names in absissae. I have no idea how to do it in Matlab.
2) I need a graph in which i plot how the mean stablizes itself as a function of the sampling.
I have 100 values and i should take the mean progressively.
For example [1 2 3 4].
I need a vector that take the mean [mean(1), mean (1+2), mean (1 + 2+ 3), mean (1+2+3+4)]. How to do that?
3) I'm comparing 2 probability density functions and i would like to know if it is possible to use different colors in the zone in common between the 2 histograms.
What Matlab does it is just to blaken the zone. I would like to know if it is possible to choose different colors.
Thanks in advance!

 採用された回答

Rik
Rik 2019 年 3 月 25 日

0 投票

For your first question: you can either use a categorical array, or set the xticks and xticklabels.
You can calculate the progressive mean with the help of cumsum:
data=rand(100,1);
%make sure to only input vectors
prog_mean=cumsum(data)./cumsum(ones(size(data)));
As for your third question, please post the code you use. It is often easier to adapt code that almost does what it needs to do, then guess your data structure and appropriate code.

5 件のコメント

Gianluca Borgna
Gianluca Borgna 2019 年 3 月 26 日
Hi Rik!
As you asked me, i put an example code.
x=0:0.1:10;
y=normpdf(x,5,0.8);
z=normpdf(x,3.5,0.5);
plot(x,y); hold on; plot(x,z);
I just would like to blacken (or other colors) the area in common between the 2 distributions.
How to do the same with 2 histograms and the distributions plotted on each?
Thanks!
Rik
Rik 2019 年 3 月 26 日
I don't have the stats toolbox, so I don't have the normpdf function, so I used two polynomials instead. I used a very coarse grid to show the shortcommings of this method. Using a finer grid will make the gap stand out less.
%generate curves
x=linspace(0,10,10);
y1=-(x-4).^2+8;y1(y1<0)=0;
y2=-(2*x-14).^2+18;y2(y2<0)=0;
shared=zeros(size(y1));
shared(y1>y2)=y2(y1>y2);%slope up
shared(y2>y1)=y1(y2>y1);%slope down
shared(y1&y2 & y1==y2)=y1(y1&y2 & y1==y2);%equal
figure(1),clf(1)
plot(x,y1),hold on,plot(x,y2)
patch(x,shared,'k')
Gianluca Borgna
Gianluca Borgna 2019 年 3 月 26 日
編集済み: Rik 2019 年 3 月 26 日
Ok, thanks! It works!
Can i ask you one more thing?
I have 4 pdfs. I'm using num2str for the legend. Each plot is named f1,f2,f3,f4.
I just would like to plot in a single legend all the statistics.
I was writing like this.
figure; histogram(af_Caramagna,CaramagnaEdges,'Normalization','pdf','FaceColor',[0.3010 0.7450 0.9330]);
hold on; f1=plot(CaramagnaEdges, Caramagna_distr, 'linewidth',2, 'color','b'); grid minor;
hold on;
histogram(af_CaramagnaDeg1,CaramagnaEdges,'Normalization','pdf','FaceColor','r');
f2=plot(CaramagnaEdges, CaramagnaDeg1_distr, 'linewidth',2, 'color','r');
legend(f1,['\mu = ' num2str(round(Caramagna_mean,2)) ' \sigma = ' num2str(round(Caramagna_std,2)) ' CoV = ' num2str(round(Caramagna_cov,2))], ...
'orientation','vertical','location','eastoutside');
legend(f2,['\mu = ' num2str(round(CaramagnaDeg1_mean,2)) ' \sigma = ' num2str(round(CaramagnaDeg1_std,2)) ' CoV = ' num2str(round(CaramagnaDeg1_cov,2))], ...
'orientation','vertical','location','eastoutside');
But if i write in this way the second legend overwrites the first one. As you can imagine i would l like to have just one legend with all the statistics.
I tried to do like:
legend(f1 f2, [.....], [....]) but it doesn't work.
Rik
Rik 2019 年 3 月 26 日
I prefer setting the DisplayName property in my calls to plot and then only turning the legend on. You can also do it differently, please read the doc for the legend function. Matlab documentation is actually pretty good. You will find several examples there, including the allowed forms of syntax.
The code below should give you an idea as well. Also: try using arrays of handles, instead of numbered variables. Numbered variables are generally a bad idea.
f=[];%clear f in case it already exists to prevent unexpected outcomes
figure; histogram(af_Caramagna,CaramagnaEdges,'Normalization','pdf','FaceColor',[0.3010 0.7450 0.9330]);
hold on; f(1)=plot(CaramagnaEdges, Caramagna_distr, 'linewidth',2, 'color','b'); grid minor;
hold on;
histogram(af_CaramagnaDeg1,CaramagnaEdges,'Normalization','pdf','FaceColor','r');
f(2)=plot(CaramagnaEdges, CaramagnaDeg1_distr, 'linewidth',2, 'color','r');
legend(f,...
['\mu = ' num2str(round(Caramagna_mean,2)) ' \sigma = ' num2str(round(Caramagna_std,2)) ' CoV = ' num2str(round(Caramagna_cov,2))], ...
['\mu = ' num2str(round(CaramagnaDeg1_mean,2)) ' \sigma = ' num2str(round(CaramagnaDeg1_std,2)) ' CoV = ' num2str(round(CaramagnaDeg1_cov,2))], ...
'orientation','vertical','location','eastoutside');
Gianluca Borgna
Gianluca Borgna 2019 年 3 月 26 日
Thanks Rik! It's exactly what i wanted!

サインインしてコメントする。

その他の回答 (0 件)

製品

リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by