How to add Mean and Median to a Histogram

203 次查看(过去 30 天)
I have created a histogram from the array labelled DistanceMiles using the command histogram(DistanceMiles) . I need to find the mean and indicate it on the plot with a red "x", and find the median, and indicate that with a black "o"
  3 个评论
Rik
Rik 2021-4-11
Why did you delete the question? Now the answer doesn't make sense anymore. Please restore it.

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2021-4-11
编辑:Image Analyst 2021-4-11
The original question, before @Michael Kuhlow deleted it for some reason, asked how to indicate mean and median on a graph of the histogram of some data. Mean with a red x, and median with a black o.
Michael, try this. Adapt as needed.
data = randn(100, 1) + 10;
h = histogram(data)
dataMean = mean(data(:))
dataMedian = median(data(:))
grid on;
hold on;
% Put up vertical lines there
xline(dataMean, 'Color', 'r', 'LineWidth', 2);
xline(dataMedian, 'Color', 'k', 'LineWidth', 2);
% Find bin centers
binCenters = (h.BinEdges(1:end-1) + h.BinEdges(2:end))/2;
% Put up red x on top of the bar
[~, index] = min(abs(dataMean - binCenters))
plot(binCenters(index), h.Values(index), 'rx', 'LineWidth', 3, 'MarkerSize', 20);
[~, index] = min(abs(dataMedian - binCenters))
plot(binCenters(index), h.Values(index), 'ko', 'LineWidth', 3, 'MarkerSize', 20);
  2 个评论
Juan Sebastian
Juan Sebastian 2024-1-13
编辑:Juan Sebastian 2024-1-13
How can I estimade the mode for the histogram? I know that it is necesarry to estimate this parameters for data grouped.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Histograms 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by