Histogram shows one value at the very end that ruins the plot

1 次查看(过去 30 天)
Hello I have a vactor of values and I am ploting a histogram. I get one value at the very end that is ruining my plot. I cannot get rid of this. Can you please help.
My code is simple and is below. I have also attached data file RT160 in .mat format. Please help.
figure(1);
histogram(RT160,150);
xlabel('Cost (USD)');
ylabel('Counts');

回答(1 个)

Steven Lord
Steven Lord 2022-3-9
If that last bin is (roughly) twice as high as you think it should be, that's because the last bin includes both values that match its left bin edge and values that match its right bin edge. The rest of the bins include just their left bin edge (leaving their right bin edge to their neighbor to the right.)
x = randi(10, 1, 1e3);
figure
histogram(x, 1:10) % Last bin contains both 9 and 10
figure
histogram(x, 1:11) % Last bin is [10, 11] which matches only 10 in x
I'd just add one element at the end of my bin edges, the max of my data plus my desired BinWidth.
desiredBinWidth = 1;
newUpperLimit = max(x) + desiredBinWidth
newUpperLimit = 11

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by