How to place percent of each bar/bin of histogram on histogram chart in the code below?

조회 수: 74 (최근 30일)
Hi all,
I have a code like this. Could you guys please help me to plot the percent of total for each histogram bar exactly on top of it? also, how to plot the mean in the middle of the chart instead of writing values manually for text command.
I really really appreciate your help.
histogram(Data,'BinEdges',edges,'Normalization','probability','FaceColor','#D95319','FaceAlpha',1); %, 'DisplayStyle','stairs'
ytix = get(gca, 'YTick'); % setting y in %
set(gca, 'YTick',ytix, 'YTickLabel',ytix*100);% setting y in %
MeanPlot = mean(Data);% calculating mean
text(4.5,0.4,(['mean = ',num2str(MeanPlot,3)])); % plotting mean on chart ; here I want to have that position exactly at the middle of the graph on top of the highest value
set(gca,'xtick',1:5,...
'xticklabel',{'Strongly disagree','Disagree','Neutral','Agree','Strongly agree'}); %creating x labels
ylabel('Percent (%)'); % y label
% set(findall(gcf,'-property','FontSize'),'FontSize',15);
title('Sample Chart for "Chartester"'); % title

채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 6월 6일
Instead of histogram, I suggest you use histcounts along with bar:
% test data
data = rand(1,1000);
hc = histcounts(data);
b = bar(hc);
% percent of total for each bar
s = compose('%.1f%%', hc / sum(hc) * 100);
yOffset = 5; % tweat, as necessary
text(b.XData, b.YEndPoints + yOffset,s);
  댓글 수: 2
Wolfgang McCormack
Wolfgang McCormack 2021년 6월 6일
@Scott MacKenzie thank you so much Scott, I will change my code to histcounts and bar. Just one more question, how do I place the mean of all data right between 5 and 6 and at the top of the chart (right below the top edge).
Also, since I have so many questions to analize, is there any possibility to add a parametric value to the yoffset?
Thanks in advance
Scott MacKenzie
Scott MacKenzie 2021년 6월 6일
Although there are more elegant, data-independent ways to do this, you can just put something like the following code after the bar function:
set(gca,'ylim', [0 150]); % make some room at the top
s1 = sprintf('Grand mean = %.3f', mean(data));
text(5.5, 140, s1); % choose y-coord to get proper positioning

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

추가 답변 (1개)

Steven Lord
Steven Lord 2021년 6월 6일
Let's make some sample data and plot a histogram with a small number of bins.
x = randn(1, 1e4);
h = histogram(x, 'NumBins', 10);
I'm going to extract two of the histogram properties to their own variables to save on typing.
edges = h.BinEdges;
values = h.Values;
The center of each bin is its left edge plus half the distance to the right edge (which is where diff comes in.)
centers = edges(1:end-1)+diff(edges)/2;
Now let's put the text 10% above the top of each bin. You may want to tweak the actual y values, since the labels for the shortest bins will overlap the bins themselves.
text(centers, 1.1*values, string(values), 'HorizontalAlignment', 'center')
Adjust the limits of the axes to fit the highest bar's label.
ylim(ylim.*[1 1.15])
  댓글 수: 2
Wolfgang McCormack
Wolfgang McCormack 2021년 6월 7일
@Steven Lord wow thanks but this is the count. How do I show them in percentage?
Steven Lord
Steven Lord 2021년 6월 7일
Specify the 'Normalization' name-value pair in your histogram call and you'd probably want to call sprintf instead of just string in the text call.

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

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by