How do I label the bars in my histogram with the function "histogram" ?

In a similar thread a user asked: How do I label the bars in my histogram?
However, that person mentioned the function hist, instead of the function histogram, and the solution proposed by @MathWorks Support Team was therefore based on the function hist:
% Create random data for the sake of the example
data = 10*rand(1,100);
% Draw the histogram
hist(data);
% Get information about the same
% histogram by returning arguments
[n,x] = hist(data);
% Create strings for each bar count
barstrings = num2str(n');
% Create text objects at each location
text(x,n,barstrings,'horizontalalignment','center','verticalalignment','bottom')
Now, how do I label my bars but using the function histogram instead of hist ?
Indeed, the function histogram has not "counts" and "centers" as the function hist:
[counts,centers] = hist(___)

 Réponse acceptée

Voss
Voss le 1 Juil 2022
Modifié(e) : Voss le 1 Juil 2022
% Create random data for the sake of the example
data = 10*rand(1,100);
% Draw the histogram and return histogram object
h = histogram(data);
% Get information about the histogram
edges = get(h,'BinEdges');
n = get(h,'Values');
% Create strings for each bar count
barstrings = num2str(n');
% Create text objects at each location
x = (edges(1:end-1)+edges(2:end))/2;
text(x,n,barstrings,'horizontalalignment','center','verticalalignment','bottom')

3 commentaires

Using num2str(n') to create a 2D character array of text labels can give you labels with spaces in them, which won't appear exactly centered on the bars.
To take an extreme case:
% Create random data for the sake of the example
data = 5*rand(1,11000);
% Make the majority of the data end up in a single bin
data(1:10000) = 0;
% Set up subplots for comparison of two approaches below
figure();
ax = [subplot(2,1,1) subplot(2,1,2)];
Case 1: Using num2str(n'):
% Draw the histogram and return histogram object
% (specifying number of bins = 20)
h = histogram(ax(1),data,20);
% Get information about the histogram
edges = get(h,'BinEdges');
n = get(h,'Values');
% Create strings for each bar count using num2str()
barstrings = num2str(n');
% Create text objects at each location
x = (edges(1:end-1)+edges(2:end))/2;
text(ax(1),x,n,barstrings,'horizontalalignment','center','verticalalignment','bottom')
Case 2: Using sprintfc('%d',n):
% Draw the histogram and return histogram object
% (specifying number of bins = 20)
h = histogram(ax(2),data,20);
% Get information about the histogram
edges = get(h,'BinEdges');
n = get(h,'Values');
% Create strings for each bar count using sprintfc()
barstrings = sprintfc('%d',n);
% % Alternative: Create strings for each bar count using strtrim(cellstr(num2str()))
% barstrings = strtrim(cellstr(num2str(n')))
% Create text objects at each location
x = (edges(1:end-1)+edges(2:end))/2;
text(ax(2),x,n,barstrings,'horizontalalignment','center','verticalalignment','bottom')
% Use log y-scale to see the bars better in this case
% (doesn't affect the horizontal spacing of text labels)
set(ax,'YScale','log')
% Title the plots
title(ax(1),'extra spaces')
title(ax(2),'no extra spaces')
Thanks a lot @Voss!! Cool answer!! :-)
Thank you, and you're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Question posée :

Sim
le 1 Juil 2022

Commenté :

le 3 Juil 2022

Community Treasure Hunt

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

Start Hunting!

Translated by