【问题标题】:How to turn y axis of histogram to show percentage ranging from 0 to 1如何转动直方图的 y 轴以显示从 0 到 1 的百分比
【发布时间】:2019-09-30 19:22:18
【问题描述】:

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

这是目前的样子:

这就是我想要的:

【问题讨论】:

标签: matlab histogram normalization


【解决方案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 计算并绘制直方图,然后通过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);

【讨论】:

  • 但我的意思是在 y 轴上显示从 0 到 1 的百分比。如果 i 而不是 y=y/y(1) 有 y =y/sum(长度(数据))?? @克里斯
  • @nikos: fitdist 已经返回归一化分布(整数为 1)。所以,如果你想要这样,就不要改变它的输出。
【解决方案2】:

您可以使用 y 轴设置限制

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

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

【讨论】:

  • 谢谢你,但它似乎没有像我想要的那样工作
猜你喜欢
  • 1970-01-01
  • 2013-12-27
  • 1970-01-01
  • 2017-11-21
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
相关资源
最近更新 更多