Hello,
I got data with values form 90 to 100, and i can have decimal values like 90.4 95.3. My goal is to create an histogram with binedges (i tihnk is the proper name) with 90-91, 91-92, 92-93, etc.
A = randi([90 100],50,1);
B = rand(50,1);
C = A + B;
histogram(B(:,1),'BinWidth',1);
And I would to know how to place the on top of each bar, how many values from 90 to 91, 91 to 92, etc
Thanks for your time

 채택된 답변

Steven Lord
Steven Lord 2018년 12월 5일

5 개 추천

I would not depend upon the tick locations as Luna's solution does. Use the data stored in the histogram object itself instead.
Generate some sample data and create the histogram.
data = 10*rand(1,100);
x = histogram(data,'BinWidth',1);
Retrieve the locations of the edges and the heights of the bars.
E = x.BinEdges;
y = x.BinCounts;
Find the X coordinates of the center of the bars. This takes the left edge of each bar and adds half the distance to the next bar edge.
xloc = E(1:end-1)+diff(E)/2;
Put the labels 1 unit above the top of the bar center.
text(xloc, y+1, string(y))

댓글 수: 4

Luna
Luna 2018년 12월 7일
편집: Luna 2018년 12월 7일
Right, it works fine except string. Needed mxn char array.
text(xloc, y+1, num2str(y'))
Steven Lord
Steven Lord 2018년 12월 7일
That would depend which release you're using. I tried it in release R2018b and it works. I don't remember offhand when the text function started accepting string arrays.
Luna
Luna 2018년 12월 9일
Ah OK, you are right. Mine is 2018a.
h = histogram('Categories',Categories,'BinCounts',N);
y = h.Values;
text(1:length(N), y+50, string(y));

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

추가 답변 (1개)

Luna
Luna 2018년 12월 5일

1 개 추천

Hello Tiago,
Try this for getting labels on the top of the bars of your histogram.
data = 10*rand(1,100);
x = histogram(data,'BinWidth',1);
labels = num2str(x.BinCounts');
xt = get(gca, 'XTick');
xVals = xt - ([0 diff(xt)]/2);
xVals(1) = [];
text(xVals, x.BinCounts, labels, 'HorizontalAlignment','center', 'VerticalAlignment','bottom')
But first check your data to plot histogram there is something wrong about it:
A = randi([90 100,100,1]); % look randi function and its inputs
B = rand(50,1);
C = A + B; % check A and B have the same sizes to add

댓글 수: 2

Tiago Dias
Tiago Dias 2018년 12월 5일
yeah you are right, just edit my code. will try your answer
Luna
Luna 2018년 12월 5일
편집: Luna 2018년 12월 5일
Put 'BinWidth' as 0.1 with this data. Otherwise it does not seem OK
Don't forget to accept the answer if it works :)

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

카테고리

도움말 센터File Exchange에서 Histograms에 대해 자세히 알아보기

질문:

2018년 12월 5일

댓글:

2020년 8월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by