在Matlab中对直方图进行归一化并以百分比表示y轴 [英] Normalizing a histogram and having the y-axis in percentages in matlab

查看:1697
本文介绍了在Matlab中对直方图进行归一化并以百分比表示y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,因此,我通过阅读一些较老的问题来回答了自己的问题.对于这个问题,我深表歉意!使用代码

Alright, so I answered my own question, by reading older questions a bit more. I apologize for asking the question! Using the code

Y = rand(10,1);
C = hist(Y);
C = C ./ sum(C);
bar(C)

使用相应的数据代替随机数据可以很好地工作.现在只需要优化垃圾箱大小即可.

with the corresponding data instead of the random data worked fine. Just need to optimize the bin size now.

美好的一天, 现在,我知道您一定在想这已经被问了一千遍了.在某种程度上,您可能是对的,但我在此处找到的帖子中找不到我的特定问题的答案,因此我认为我也可以问一下.我会尽量保持清楚,但是如果不清楚我要做什么,请告诉我

Good day, Now I know that you must be thinking that this has been asked a thousand times. In a way, you are probably right, but I could not find the answer to my specific question from the posts that I found on here, so I figured I might as well just ask. I'll try to be as clear as possible, but please tell me if it is not evident what I want to do

好的,所以我有一个包含5000个元素的(行)向量,所有元素都是整数.现在我想做的是绘制这5000个元素的直方图,但是以这样的方式:y轴使您有机会进入那个特定的bin中,而x轴仍然是规则的,因为它给出了该特定bin的值.

Alright, so I have a (row) vector with 5000 elements, all of which are just integers. Now what I want to do is plot a histogram of these 5000 elements, but in such a way that the y-axis gives the chance of being in that certain bin, while the x-axis is just still regular, as in it gives the value of that specific bin.

现在,对我来说有意义的是对所有内容进行规范化,但这似乎行不通,至少我是这样做的.

Now, what made sense to me was to normalize everything, but that doesn't seem to work, at least how I'm doing it.

我的第一次尝试是

sums = sum(A);
hist(sums/trapz(sums),50)

我省略了其余部分,因为它从某个文件中导入了大量数据,这并不重要. sums = sum(A)可以正常工作,我可以在Matlab中看到矢量. (控制台,我应该怎么称呼它?).但是,用trapz除以面积只会改变我的x轴,而不是我的y轴.一切都变得非常小,大约为10 ^ -3,而应该大约为10.

I omitted the rest because it imports a lot of data from a certain file, which doesn't really matter. sums = sum(A) works fine, and I can see the vector in my matlab thingy. (What should I call it, console?). However, dividing by the area with trapz just changes my x-axis, not my y-axis. Everything gets super small, on the order of 10^-3, while it should be on the order of 10.

现在环顾四周,有人建议使用

Now looking around, someone suggested to use

hist(sums,50)
ylabels = get(gca, 'YTickLabel');
ylabels = linspace(0,1,length(ylabels));
set(gca,'YTickLabel',ylabels); 

尽管这确实使y轴从0变为1,但根本没有对其进行归一化.我希望它实际上反映出被放入某个垃圾箱的机会.将两者结合起来也不起作用.抱歉,答案很明显,我看不到.

While this certainly makes the y-axis go from 0 to 1, it is not normalized at all. I want it to actually reflect the chance of being in a certain bin. Combining the two does also not work. I apologize if the answer is very obvious, I just don't see it.

尽管我意识到这是一个单独的问题(已被问过一百万次),但是我还是手动选择了容器大小,直到看起来不错为止,因为直方图中没有丢失任何条形.我已经看到了几种可以优化bin大小的脚本,但是在所有情况下,似乎都没有一个脚本能使最好的"直方图变得可悲:(如果所有数字都是,是否有一种简单的方法来选择大小?整数?

Although I realize this is a seperate question (that has been asked a million times), but the bin size I just picked by hand until it looked good, as in no bars missing from the histogram. I've seen several different scripts that are supposed to optimize bin size, but none of them seem to make the 'best' looking histogram in every case, sadly :( Is there an easy way to pick the size, if all the numbers are integers?

推荐答案

(只需关闭问题)

直方图是绝对频率图,因此所有bin频率之和(hist函数的输出向量之和)始终是其输入向量中元素的数量.因此,如果要输出百分比,则只需将输出中的每个元素除以该总数即可:

Histogram is an absolute frequency plot so the sum of all bin frequencies (sum of the output vector of hist function) is always the number of elements in its input vector. So if you want a percentage output all you need to do is dividing each element in the output by that total number:

x = randn(10000, 1);
numOfBins = 100;
[histFreq, histXout] = hist(x, numOfBins);
figure;
bar(histXout, histFreq/sum(histFreq)*100);
xlabel('x');
ylabel('Frequency (percent)');

如果要重构数据的概率密度函数,则需要考虑直方图的bin大小,然后将频率除以该值:

If you want to reconstruct the probability density function of your data, you need to take into account the bin size of the histogram and divide the frequencies by that:

x = randn(10000, 1);
numOfBins = 100;
[histFreq, histXout] = hist(x, numOfBins);
binWidth = histXout(2)-histXout(1);
figure;
bar(histXout, histFreq/binWidth/sum(histFreq));       
xlabel('x');
ylabel('PDF: f(x)');
hold on
% fit a normal dist to check the pdf
PD = fitdist(x, 'normal');
plot(histXout, pdf(PD, histXout), 'r');

更新:

自MATLAB R2014b起,您可以使用"histogram"命令轻松生成具有各种规格化的直方图.例如,上面变为:

Since MATLAB R2014b, you can use the 'histogram' command to easily produce histograms with various normalizations. For example, the above becomes:

x = randn(10000, 1);
figure;
h = histogram(x, 'normalization', 'pdf');
xlabel('x');
ylabel('PDF: f(x)');
hold on
% fit a normal dist to check the pdf
PD = fitdist(x, 'normal');
plot(h.BinEdges, pdf(PD, h.BinEdges), 'r');

这篇关于在Matlab中对直方图进行归一化并以百分比表示y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆