Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display all x-labels on 'bar' plot?

Tags:

plot

matlab

I have the following data that I wish to plot in a bar graph in MatLab:

publications = [15 12 35 12 19 14 21 15 7 16 40 28 6 13 16 6 7 22 23 16 45];
bar(publications,0.4)
set(gca,'XTickLabel',{'G1','G2','G3','G4','G5','G6','G7','G8','G9','G10',...
    'G11','G12','G14','G16','G17','G18','G19','G20','G21','G22','G23'})

However, when I execute this, I get the following plot:

enter image description here

Obviously the x-label is incorrect here as the first bar should have the x-label 'G1', the second should have 'G2', etc, until we get to the last bar which is supposed to have 'G23'.

If anyone knows how I can fix this, I would really, really appreciate it!

like image 637
Kristian Avatar asked Jul 14 '14 08:07

Kristian


People also ask

How do I show all x-axis labels in R?

To display all the labels, we need to rotate the axis, and we do it using the las parameter. To rotate the label perpendicular to the axis we set the value of las as 2, and for horizontal rotation, we set the value as 1. Secondly, to increase the font size of the labels we use cex.

How do I show all X labels in MatPlotLib?

MatPlotLib with Python To show all X coordinates (or Y coordinates), we can use xticks() method (or yticks()).

How do you stagger x-axis labels?

One way to solve this problem is to right-click the axis on the chart and select Format Axis from the shortcut menu. Then in the Format Axis task pane, under Labels, set the Label Position to Low. 21.


1 Answers

Add the following line:

set(gca,'XTick',1:numel(publications))

before you set the labels.

Now it depends how big your resulting plot is, because the labels are a little packed. You may adjust fontsize or Orientation or the gaps between the bars.

Probably the publication names are a little longer so a 90° rotation is the best and you may find this answer or this link helpful.

Another suggestion would be to use barh and rotate after print:

publications = [15 12 35 12 19 14 21 15 7 16 40 28 6 13 16 6 7 22 23 16 45];
bh = barh(publications,0.4)
set(gca','XAxisLocation','top')
set(gca,'YTick',1:numel(publications))
set(gca,'YTickLabel',{'G1','G2','G3','G4','G5','G6','G7','G8','G9','G10',...
    'G11','G12','G14','G16','G17','G18','G19','G20','G21','G22','G23'})

enter image description here

like image 155
Robert Seifert Avatar answered Oct 10 '22 17:10

Robert Seifert