How to add Mean and Median to a Histogram

조회 수: 201 (최근 30일)
Michael Kuhlow
Michael Kuhlow 2021년 4월 11일
댓글: Image Analyst 2024년 1월 13일
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.
Rena Berman
Rena Berman 2021년 5월 6일

(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

답변 (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.
Image Analyst
Image Analyst 2024년 1월 13일
@Juan Sebastian you can do
modeValue = mode(h.Values)
modeBin = h.BinEdges == modeValue

댓글을 달려면 로그인하십시오.

카테고리

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