Clear Filters
Clear Filters

Labeling bar charts with over 15 categories

4 views (last 30 days)
I'm generating a simple bar-chart with category names on the x-axis. All seems to work fine when the number of categories is 15 or less. For 16 (or more) categories, the chart plots OK but the x-axis labels are not right. I'm using MatLab 2015b.
% Set number of categories.
num_categories = 16;
% Create data.
values = rand(1,num_categories);
% Create category names.
symbols = ['a':'z'];
string_length = 5;
for i = 1:num_categories
nums = randi(numel(symbols),[1 string_length]);
category_name(i) = cellstr(symbols(nums));
end
% Plot bar-chart.
bar(values)
set(gca,'XTickLabel',category_name)
set(gca,'XTickLabelRotation',90)

Accepted Answer

Cedric
Cedric on 11 Sep 2017
Edited: Cedric on 11 Sep 2017
Your code uses the former ticks. Just add the following to define ticks that match the number of bars/categories and the default position of the bars:
set(gca,'XTick',1:num_categories)

More Answers (0)

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!