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

40 次查看(过去 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
@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])

类别

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