Percentage values in scatterhist histogram

조회 수: 5 (최근 30일)
K.
K. 2020년 9월 25일
편집: Adam Danz 2020년 9월 28일
Hello all,
I need a help with scatterhist and the information on each histogram.
For example lets have the next plot:
scatterhist(log(rand(1,100)),log(rand(1,100)),'Marker','o','Direction','out');
I would like to add (or get) values above the histogram bars; the percentage of the distribution.
Please, is anyone could help on it?
Thank you
  댓글 수: 4
Rik
Rik 2020년 9월 25일
If you don't care about the x-y-relation, why don't you simply make a histogram out of each vector separately?
Adam Danz
Adam Danz 2020년 9월 28일
편집: Adam Danz 2020년 9월 28일
"I do not care so much for the plot. Do you know how to calculate?"
I added my answer before seeing that you didn't care about the plot but the answer still shows how to get those percentages. Fortunately the historgrams are already normalized to add up to 1 so you just need to access their "Values" properties and then multiply by 100.

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

채택된 답변

Adam Danz
Adam Danz 2020년 9월 25일
편집: Adam Danz 2020년 9월 25일
The histograms are normalized which means their bar heights are already a percentage.
The percentage of each bar can be found in the demo below; look for
  • xHist.Values.*100
  • yHist.Values.*100
Here's now to access those axes and add text elements.
% Use output to get axis handles
h = scatterhist(log(rand(1,100)),log(rand(1,100)),'Marker','o','Direction','out');
% Get hist axis handles
xHist = findobj(h(2),'Type','histogram');
yHist = findobj(h(3),'Type','histogram');
% increase ylim/xlim to make room of text labels
h(2).YLim(2) = h(2).YLim(2).*1.2; % 20% increase;
h(3).YLim(2) = h(3).YLim(2).*1.2; % 20% increase;
% Compute bin centers and labels, add text
xBinCenter = xHist.BinEdges(2:end)-xHist.BinWidth/2;
xHistLabels = compose('%.0f%%',xHist.Values.*100);
text(h(2),xBinCenter, xHist.Values.*1.01, xHistLabels,...
'HorizontalAlignment', 'Right', 'VerticalAlignment', 'middle', ...
'FontSize', 8, 'Rotation', 90)
yBinCenter = yHist.BinEdges(2:end)-yHist.BinWidth/2;
yHistLabels = compose('%.0f%%',yHist.Values.*100);
text(h(3),yBinCenter, yHist.Values*1.01, yHistLabels,...
'HorizontalAlignment', 'Right', 'VerticalAlignment', 'Middle', ...
'FontSize', 8)
If you want to see the histogram axes,
axis(h(2), 'on')
axis(h(3), 'on')

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by