【问题标题】:create bins based on a range of values for histogram figure根据直方图的一系列值创建箱
【发布时间】:2019-04-29 12:42:19
【问题描述】:

我正在做一些分析,需要生成一个直方图。我知道如何创建标准直方图,但我需要如下图所示的内容,其中每个点都是 x 轴上的一个区间。例如,每个 bin 都基于 x-x 中的一个值。

【问题讨论】:

  • @rinkert ,同意欺骗,但该答案不使用histogram,现在推荐使用该答案使用的histc
  • @SecretAgentMan,好的收回我的标志,因为不推荐其他答案。然后可以在此处或其他帖子中添加使用 histc 的新答案。
  • 我不确定你们都指的是什么答案
  • @rinkert,同意,我认为答案在于使用histogram 并调整histogram properties,但现在没有时间做完整的质量答案。
  • 相关帖子(但不重复)here 但该答案使用了 Matlab 不再推荐的功能。

标签: matlab histogram matlab-figure


【解决方案1】:

您可以使用histogram 函数,然后相应地设置XTick 位置和XTickLabels。请参阅代码中的 cmets 进行解释。

% random normally distrubuted data
x = 1*randn(1000,1);
edges = -5:1:5;

% create vector with labels (for XTickLabel ... to ...)
labels = [edges(1:end-1); edges(2:end)];
labels = labels(:);

% plot the histogram
figure();
ax = axes;
h = histogram(x, 'BinEdges', edges, 'Normalization', 'Probability');

ax.XTick = edges + mean(diff(edges)/2);
ax.XTickLabel = sprintf('%.1f to %.1f\n', labels);
ax.XTickLabelRotation = 90;

% set yticks to percentage
ax.YTickLabel = cellfun(@(a) sprintf('%i%%', (str2double(a)*100)), ax.YTickLabel, 'UniformOutput', false);

% text above bars
bin_props = h.BinCounts/numel(x);  % determine probabilities per bin in axis units
bin_centers = ax.XTick(1:end-1);  % get the bin centers

txt_heigts = bin_props + 0.01; % put the text slightly above the bar
txt_labels = split(sprintf('%.1f%% ', bin_props*100), ' ');
txt_labels(end) = [];  % remove last cell, is empty because of split.
text(ax, bin_centers, txt_heigts, txt_labels, 'HorizontalAlignment', 'center')

% set ylim to fit all text (otherwise text is outside axes)
ylim([0 .4]);

将文本放在正确的位置可能需要一些调整。最重要的是'HorizontalAlignment' 选项,以及到条形的距离。我还使用了histogram 函数中的'Normalization''probability' 选项,并将y 轴设置为也显示百分比。

我认为您可以在需要时自己在下方添加。


当您的数据可能在定义的binedges 之外时,您可以剪裁数据,并将XTickLabels 设置为小于或大于符号。

% when data can be outside of defined edges
x = 5*randn(1000,1);
xclip = x;
xclip(x >= max(edges)) = max(edges);
xclip(x <= min(edges)) = min(edges);

% plot the histogram
figure();
ax = axes;
h = histogram(xclip, 'BinEdges', edges);

ax.XTick = edges + mean(diff(edges)/2);
ax.XTickLabel = sprintf('%.1f to %.1f\n', labels);
ax.XTickLabelRotation = 90;

% set boundary labels
ax.XTickLabel{1} = sprintf('\\leq %.1f', edges(2));
ax.XTickLabel{end-1} = sprintf('\\geq %.1f', edges(end-1));


您还可以将外边缘设置为-InfInf,正如user2305193 指出的那样。因为外面的箱子要宽得多(因为它们实际上延伸到 x 轴上的Inf),您可以通过设置轴xlim 来纠正它。默认情况下,XTickLabels 将显示-Inf to -5.0,我个人不喜欢这样,所以我将它们设置为小于(等于)和大于符号。

step = 1;
edges = -5:step:5;                                  % your defined range
edges_inf = [-Inf edges Inf];                       % for histogram
edges_ext = [edges(1)-step edges];                  % for the xticks

x = 5*randn(1000,1);

% plot the histogram
figure();
ax = axes;
h = histogram(x, 'BinEdges', edges_inf, 'Normalization', 'probability');

labels = [edges_inf(1:end-1); edges_inf(2:end)];
labels = labels(:);

ax.XTick = edges_ext + step/2;
ax.XTickLabel = sprintf('%.1f to %.1f\n', labels);
ax.XTickLabelRotation = 90;

% show all bins with equal width (Inf bins are in fact wider)
xlim([min(edges)-step max(edges)+step])

% set boundary labels
ax.XTickLabel{1} = sprintf('\\leq %.1f', edges(1));
ax.XTickLabel{end-1} = sprintf('\\geq %.1f', edges(end));

【讨论】:

  • 太棒了,这正是我所需要的。有没有像我附上的原始数字那样显示百分比。我知道如何通过归一化来归一化轴 - 概率。
  • @VBA_Novice,查看normalization 属性。在调用histogram 函数时选择您的规范化方法并使用该属性。
  • 优秀的答案。回答 OP 的问题并为未来的访客明确。 (+1)
  • @Hoki 我知道如何在调用直方图函数时进行标准化。我要问的是:是否有在每个垃圾箱顶部显示百分比,如我发布的图片所示。
  • 我同意这是一个很好的回应。它帮助了我。谢谢@rinkert
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
  • 2020-09-10
  • 2012-10-19
  • 2016-02-21
相关资源
最近更新 更多