Hi,
I construct a histogram with Matlab's "bar" function. The bins are constructed as follows:
n = 8;
binranges = linspace(min(values), max(values),n+1);
"values" are just some measured values which I want to plot as bars. Now I want to label all the bins accordingly with their ranges. So if for instance binranges gives me the following:
binranges =
Columns 1 through 8
0.7175 0.7447 0.7718 0.7990 0.8262 0.8533 0.8805 0.9077
Column 9
0.9348
I would like to have the bars labeled as "0.7175-0.7447", "0.7447-0.7718" and so on.. For including a dash I guess I need to convert everything to strings as well, which I then could use as an input to the following statement:
set(gca, 'XTickLabel',...)
How do I do that most efficiently? Many thanks

 Accepted Answer

set(gca, 'XTic',binranges)
will use the bin values for ticks.
It's not as trivial to label the bin ranges as tick labels are associated with the location of the associated ticks rather than between, plus you'll almost certainly run out of room for the amount of text you're trying to write.
But, it's simple-enough to build a string array to use
>>labels=num2str([[0 binranges].' [binranges inf].'],'%0.2f-%0.2f')
labels =
0.00-0.72
0.72-0.74
0.74-0.77
0.77-0.80
0.80-0.83
0.83-0.85
0.85-0.88
0.88-0.91
0.91-0.93
0.93-Inf
>>

More Answers (2)

Jiro Doke
Jiro Doke on 8 Dec 2016
Edited: Jiro Doke on 8 Dec 2016
A number of ways, but here is one:
label = arrayfun(@(x,y) sprintf('%g - %g',x,y), ...
binranges(1:end-1), binranges(2:end), ...
'UniformOutput', false);
Then, set the axes properties.
set(gca,'XTick',(binranges(1:end-1)+binranges(2:end))/2,...
'XTickLabel',label,...
'XTickLabelRotation',30)

8 Comments

Many thanks - the weird thing is though that all labels end up under the first bin (so that it is not readable at all). Why is that?
Here all my code so far (I calculate the number of correct answers per bin):
n = 8; % set number of bins
correctAnswers = double(correctAnswers);
bins = discretize(staircase, linspace(min(staircase), max(staircase), n+1));
% Determine bin ranges
binranges = linspace(min(staircase), max(staircase),n+1);
bar(arrayfun(@(x) sum(correctAnswers(x==bins))/size(staircase(x==bins), 2), min(bins):max(bins)));
% label bins
label = arrayfun(@(x,y) sprintf('%g - %g',x,y), ...
binranges(1:end-1), binranges(2:end), ...
'UniformOutput', false);
% set labels
set(gca,'XTick',(binranges(1:end-1)+binranges(2:end))/2,...
'XTickLabel',label,...
'XTickLabelRotation',30)
dpb
dpb on 9 Dec 2016
Edited: dpb on 10 Dec 2016
If you don't create the string as either cell array or column vector of strings they'll all be run together as a single string. It's the (mandatory) reason I arranged the binranges as a column vector for the example; that causes num2str to build the column output array.
You mean leaving out "XTickLabelRotation" - I did that but it does not solve the problem. With your solution the problem is that the ranges do not start from 0 nor do they go to infinity...
Ok I got it! Using a combination of the codes - many thanks:
labels=num2str([[binranges(1:end-1)].' [binranges(2:end)].'],'%0.2f-%0.2f')
% set labels
set(gca,'XTickLabel',labels,'XTickLabelRotation',30)
dpb
dpb on 10 Dec 2016
"... ranges do not start from 0 nor do they go to infinity"
Ah, but they do! Perhaps the data don't, but the first/last bin are actually unbounded on left/right, respectively...
I did not know that! Many thanks...!!
a a
a a on 29 Aug 2017
Thanks!! I've tried for hours until I found your solution
Angeline
Angeline on 14 Mar 2025
Edited: Angeline on 14 Mar 2025
Ah, still relevant, just as 'a a' said, I've been trying to achieve this for hours 🙈 Thanks so much, this worked in one go!

Sign in to comment.

Try creating a categorical histogram (requires release R2015b or later.)
% Create some sample data
values = [0.7175 0.9348];
n = 8;
binranges = linspace(min(values), max(values),n+1);
x = values(1)+(values(2)-values(1))*rand(1, 100);
% Create a categorical array from the data
% Use binranges to define categories
C = discretize(x, binranges, 'categorical');
% Create the categorical histogram
histogram(C)
The format of the categories / tick labels are different than what you suggested but in my opinion the meaning of those labels should be clear. If you have a hard requirement to use the specific format you described you could use Jiro's arrayfun call to create a list of category names then pass that into discretize after the 'categorical' flag.

Categories

Tags

Asked:

on 8 Dec 2016

Edited:

on 14 Mar 2025

Community Treasure Hunt

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

Start Hunting!