如何旋转直方图的y轴以显示从0到1的百分比

时间:2019-05-13 19:49:39

标签: matlab histogram normalization

我想更改直方图的y轴以显示从0到1的百分比。这是我尝试过的方法,但似乎不起作用。

    myTolerance=1e-12; % in erg units.
      nbins=50;
    for j=1:ntM/100:ntM

       H = histfit(Wkinet(abs(Wkinet(:,j))>myTolerance, j) * erg2eV, nbins);
    %Select from column j all rows in column j whose absolute values are 
    %greater than the tolerance.
    H(1).delete; %%Remove bins, only keep the fit.
       set(gca, 'YScale', 'log');
        set(gca, 'XScale', 'log'); % Make logarithmic X
         yt = get(gca, 'YTick');
        set(gca, 'YTick', yt, 'YTickLabel', 
        yt/numel(Wkinet(abs(Wkinet(:,j))>myTolerance)))

      pause;
   end

这是当前的样子:

Output Graph

这就是我想要的:

Desired output graph

2 个答案:

答案 0 :(得分:2)

为简化下面的讨论,该行

H = histfit(Wkinet(abs(Wkinet(:,j))>myTolerance, j) * erg2eV, nbins);

等同于

data = Wkinet(abs(Wkinet(:,j))>myTolerance, j) * erg2eV;
H = histfit(data, nbins);

这意味着下面我们将假设data是一个向量。


histfit通过histogram计算并绘制直方图,然后通过fitdist对其拟合函数。由于您不想绘制直方图本身,因此请坚持使用fitdist

pd = fitdist(data,'Normal'); % this is the default distribution used in `histfit`, is it correct?
x = linspace(min(data),max(data),200); % 200 points in the graph, you might want to change this?
y = pdf(pd,x);
plot(x,y);

现在很容易根据需要对图进行归一化。例如,将第一个元素设置为1:

pd = fitdist(data,'Normal');
x = linspace(min(data),max(data),200);
y = pdf(pd,x);
y = y/y(1);    % <<< Normalize
plot(x,y);

答案 1 :(得分:0)

您可以使用设置y轴上的限制

ylim([1e-3 1]) %lower limit is nonzero since it's plotted on log scale

set(gca, 'ylim', [1e-3 1])