简体   繁体   English

直方图中的Matlab图

[英]Matlab plot in histogram

Assume y is a vector with random numbers following the distribution f(x)=sqrt(4-x^2)/(2*pi) . 假设y是一个遵循分布f(x)=sqrt(4-x^2)/(2*pi)具有随机数的向量。 At the moment I use the command hist(y,30) . 目前,我使用命令hist(y,30) How can I plot the distribution function f(x)=sqrt(4-x^2)/(2*pi) into the same histogram? 如何将分布函数f(x)=sqrt(4-x^2)/(2*pi)绘制到同一直方图中?

Let's take an example of another distribution function, the standard normal. 让我们以另一个分布函数(标准正态)为例。 To do exactly what you say you want, you do this: 要完全按照您的意愿进行操作,请执行以下操作:

nRand = 10000;
y = randn(1,nRand);
[myHist, bins] = hist(y,30);
pdf = normpdf(bins);
figure, bar(bins, myHist,1); hold on; plot(bins,pdf,'rx-'); hold off;

This is probably NOT what you actually want though. 这可能不是您真正想要的。 Why? 为什么? You'll notice that your density function looks like a thin line at the bottom of your histogram plot. 您会注意到,密度函数在直方图底部看起来像一条细线。 This is because a histogram is counts of numbers in bins, while a density function is normalized to integrate to one. 这是因为直方图是对二进制数进行计数,而密度函数被归一化为一个整数。 If you have hundreds of items in a bin, there is no way that the density function will match that in scale, so you have a scaling or normalization problem. 如果箱中有数百个项目,则密度函数将无法与比例尺相匹配,因此会出现缩放或归一化问题。 Either you have to normalize the histogram, or plot a scaled distribution function. 您必须对直方图进行归一化,或者绘制比例分布函数。 I prefer to scale the distribution function so that my counts are sensical when I look at the histogram: 我更喜欢对分布函数进行缩放,以便在查看直方图时,我的计数是有意义的:

normalizedpdf = pdf/sum(pdf)*sum(myHist);
figure, bar(bins, myHist,1); hold on; plot(bins,normalizedpdf,'rx-'); hold off;

Your case is the same, except you'll use the function f(x) you specified instead of the normpdf command. 您的情况是一样的,除了将使用指定的函数f(x)代替normpdf命令。

Instead of normalizing numerically, you could also do it by finding a theoretical scaling factor as follows. 除了对数值进行归一化,您还可以通过以下方法找到理论比例因子来实现。

nbins = 30;
nsamples = max(size(y));
binsize = (max(y)-min(y)) / nsamples
hist(y,nbins)
hold on
x1=linspace(min(y),max(y),100);
scalefactor = nsamples * binsize 
y1=scalefactor * sqrt(4-x^2)/(2*pi)
plot(x1,y1)

Update: How it works. 更新: 工作原理。

For any dataset that is large enough to give a good approximation to the pdf (call it f(x)), the integral of f(x) over this domain will be approximately unity. 对于任何足够大以能够很好地近似pdf的数据集(称为f(x)),在该域上f(x)的积分将近似为1。 However we know that the area under any histogram is precisely equal to the total number of samples times the bin-width. 但是,我们知道任何直方图下的面积正好等于样本总数乘以bin宽度。

So a very simple scale factor to bring the pdf into line with the histogram is Ns*Wb, the total number of sample point times the width of the bins. 因此,使pdf与直方图一致的一个非常简单的比例因子是Ns * Wb,即采样点总数乘以分格宽度。

Let me add another example to the mix: 让我添加另一个示例:

%# some normally distributed random data
data = randn(1e3,1);

%# histogram
numbins = 30;
hist(data, numbins);
h(1) = get(gca,'Children');
set(h(1), 'FaceColor',[.8 .8 1])

%# figure out how to scale the pdf (with area = 1), to the area of the histogram
[bincounts,binpos] = hist(data, numbins);
binwidth = binpos(2) - binpos(1);
histarea = binwidth*sum(bincounts);

%# fit a gaussian
[muhat,sigmahat] = normfit(data);
x = linspace(binpos(1),binpos(end),100);
y = normpdf(x, muhat, sigmahat);
h(2) = line(x, y*histarea, 'Color','b', 'LineWidth',2);

%# kernel estimator
[f,x,u] = ksdensity( data );
h(3) = line(x, f*histarea, 'Color','r', 'LineWidth',2);

legend(h, {'freq hist','fitted Gaussian','kernel estimator'})

历史

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM