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

How to turn y axis of histogram to show percentage ranging from 0 to 1

我想更改直方图的 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

这是目前的样子:

这就是我想要的:

您可以使用

为您的 y-axis 设置限制

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

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

为了简化下面的讨论,行

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, then fits a function to it through 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);