How to rescale the y-axis of a histogram?

조회 수: 23 (최근 30일)
Michael
Michael 2019년 11월 29일
답변: Image Analyst 2019년 11월 29일
Let' say that we have 100 data points, and we’re making a histogram.
[counts] = histcounts(data)
sum(counts) should equal to 100.
If I were to normalize the histogram, in other words, we normalize the y-axis, the total count would equal to 1.
Notice here I am not changing the original data, I am simply changing the counts or the y-scale.
Now, I would like to rescale the y-axis, such that the total count would equal 200.
The new plot should follow the same distribution as before, except it has a different y-scale.
Is there a way to do this?

채택된 답변

Star Strider
Star Strider 2019년 11월 29일
One approach:
X = randn(1, 420);
[N,edges] = histcounts(X, 'Normalization', 'probability');
xbar = edges(1:numel(N)) + mean(diff(edges))/2;
figure
bar(xbar, N)
grid
yt = get(gca, 'YTick');
ytix = linspace(min(yt), max(yt), 10);
set(gca, 'YTick',ytix, 'YTickLabel',fix(ytix*200/max(yt)))
Experiment to get different results.
  댓글 수: 2
Michael
Michael 2019년 11월 29일
nice
Star Strider
Star Strider 2019년 11월 29일
Thank you!

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

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 11월 29일
You can use ylim() to scale the y axis. Use it to set the min and max value of the y axis to whatever you want them to be.

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by