How to specify your own (not numerical) categories on the x-axis of a box plot?

69 views (last 30 days)
I want to create a box plot where on the x-axis I have not numbers but categories, e.g. countries (France, Belgium, Netherlands etc). The y-axis just numerical, e.g., the box height represents GDP per person in a particular country. How do I specify the x-axis?

Accepted Answer

the cyclist
the cyclist on 11 Sep 2013
Here is one way
countries = {'Botswana','Lesotho','Iceland'};
x1 = randn(1000,3);
x2 = rand(1,3);
figure
boxplot(x1)
set(gca,'XTick',1:3,'XTickLabel',countries)
figure
bar(x2)
set(gca,'XTick',1:3,'XTickLabel',countries)
You said "box plot", but what you described sounds more like a "bar chart", so I gave you a simple example of each.
  3 Comments
Svetlana
Svetlana on 11 Sep 2013
Thanks a lot for this answer, I tried it and it works but now I have a next problem: I have 34 countries and clearly all names do not fit on x-axis if they are written horizontally. Can I rotate them, ie write the names "vertically", if you know what I mean?
the cyclist
the cyclist on 11 Sep 2013
It's not super-easy. See this page for some suggestions on that:
Personally, I like the xticklabel_rotate function that is mentioned about halfway down that page.

Sign in to comment.

More Answers (1)

Sabiha Mahzabeen
Sabiha Mahzabeen on 29 Nov 2018
How can i get histogram where y-axis with percentages and x-axis with categorical variables?
Airline={'Air Canada', 'Delta Airline','American Airline'};
y lebel=[.20 .35 .25 ];

Categories

Find more on Axes Appearance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!