根据直方图值的范围创建垃圾箱

时间:2019-04-29 12:42:19

标签: matlab histogram matlab-figure

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

Example histogram

1 个答案:

答案 0 :(得分:4)

您可以使用histogram功能,然后分别设置XTick位置和XTickLabels。请参阅代码中的注释以获取解释。

% 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'选项以及与条形的距离。我还使用了'Normalization'函数中的'probability'histogram选项,并将y轴设置为也显示百分比。

我认为您可以根据需要在自己下方添加内容。

enter image description here


当数据可以位于已定义的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));

enter image description here


您也可以将外部边缘设置为-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));

enter image description here