Displaying names vertically on the x axis

26 views (last 30 days)
I am doing a school project on environmental analysis and was trying to compare various factors such as carbon emission energy consumption, forest area and population. I have managed to plot a simple 2D graph as below but am keen to place the names of the countries at the bottom of each bar vertically @ the x axis point. Would that be possible ?
Also i felt that the way i am visualizing my data is rather bad or unclear. Is there a better way like adding colored bar or using graphs or 3d scatter ?
My Code would be as below..
%1-21, 22-42, 43-63, 64-84%
combine = table(C1990, C1991, C1992, C1993, C1994, C1995, C1996, C1997, C1998, C1999, C2000, C2001, C2002, C2003, C2004, C2005, C2006, C2007, C2008, C2009, C2010,...
F1990, F1991, F1992, F1993, F1994, F1995, F1996, F1997, F1998, F1999, F2000, F2001, F2002, F2003, F2004, F2005, F2006, F2007, F2008, F2009, F2010,...
E1990, E1991, E1992, E1993, E1994, E1995, E1996, E1997, E1998, E1999, E2000, E2001, E2002, E2003, E2004, E2005, E2006, E2007, E2008, E2009, E2010,...
P1990, P1991, P1992, P1993, P1994, P1995, P1996, P1997, P1998, P1999, P2000, P2001, P2002, P2003, P2004, P2005, P2006, P2007, P2008, P2009, P2010,...
'RowNames', Country)
year = 1990:1:2010;
h1 = combine(1:59 ,:)
h2 = combine(:, { 'C1990' 'F1990' 'E1990' 'P1990' })
% Draw a bar chart for a number of variables
vars = { 'C1990' , 'F1990' , 'E1990' , 'P1990' }
ylabels = {'Carbon Emission' 'Forest Land Area' 'Energy COnsumption' 'Population'};
for i = 1: length( vars )
% field/variable names for structures/datasets must be char arrays
variable = char( vars (i) );
% dynamically reference dataset variable
data = combine.( variable );
figure;
bar( data );
set(gca,'XTickLabel',{'Afghanistan','Argentina','Australia','Austria','Belgium','Bangladesh','Brazil','Canada','Switzerland','Chile','China','Czech Republic','Germany','Denmark','Egypt','Spain','Finland','France','United Kingdom','Georgia','Greece','Hong Kong','Hungary','Indonesia','India','Ireland','Iran','Iraq','IceLand','Israel','Italy','Jamaica','Jordan','Japan','Cambodia','Sri Lanka','Maldives','Myanmar','Malaysia','Netherlands','Norway','Nepal','New Zealand','Pakistan','Peru','Philippines','Poland','Portugal','Paraguay','Russia','Saudi Arabia','Singapore','Sweden','Thailand','Turkey','Ukraine','United States','Vietnam','South Africa','World'})
title( ylabels(i) );
%xlabel( 'x' );
ylabel( ylabels(i) );
end

Accepted Answer

dpb
dpb on 13 Apr 2014
bar first -- call bar with the data in an array and use the 'grouped' option. See examples at
doc bar
for options within the basic capabilities of bar
As for the titles, unfortunately 'xticklabel' property doesn't honor TeX interpreter so you can't add rotation or such to them if you use tick labels. You can, however, set the 'xticklabel' property to [] to not display them and then use text to write the labels where you wish and with TeX interpreter properties.
text(x,y,xtiklabs,'rotation',90)
where xtiklabs is your above hardcoded labels array and x, y are the position vectors of the locations at which you wish to place the text.
doc text % and links therein will provide examples and details...
Enjoy... :)
  3 Comments
dpb
dpb on 14 Apr 2014
Well, they were drawn where you told them to be I'm sure... :)
W/O code it's not possible to do any real debugging.
I'm also not sure what precisely you want; it's a lot of stuff to wade through to read only w/o more description of what you expect.
As noted, you need to use bar with the full array of data you want plotted in one call and the optional 'grouped' option to separate the groups. Problem is, reading a lot and trying to figure out what you really want is more time invested than I have to spend.
'Splain more and particularly would be good to cut the dataset size down drastically -- a small set works for demo purposes as well as large and is much easier to figure out.
Again, I commend to you the doc for bar and the examples, particularly.
dpb
dpb on 14 Apr 2014
OK, I had a little time...not having your data nor a version with table, here's a little sample...
d=10000*rand(8,4); % some data of four variables for eight countries
cntry={'Afghanistan','Argentina','Australia','Austria','Belgium','Bangladesh','Brazil','Canada'};
bar(d,'grouped') % the bar graph grouped by country
p=get(gca,'position'); % get the axes position vector
p(2)=p(2)+.1; p(4)=p(4)-.1; % raise bottom, shorten height for label room
set(gca,'position',p)
text(1:8,zeros(1,8)-100,cntry,'rotation',90,'horizontal','right')
From there you should see the general idea to get the effect you're wanting. Note the axis x-values are still 1:8 at the center of the bars; if you were to try to label each bar with the variable, say, you have to adjust the x-positions to be at the middle of each bar which is the integer mean value plus/minus a small delta offset.

Sign in to comment.

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!