Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab - How to use strings instead of numbers in bar figure

I want to obtain a Matlab figure using the bar function but when the actual figure is plotted instead of displaying the numbers underneath each bar I want to display which represents that actual value plotted.

For example I am having the vector x = [1 2 3] and instead of showing 1, 2 and 3 beneath each plotted bar, I want to display a string corresponding to one of these values y = {'sam'; 'alan'; 'ellie'}

enter image description here

Could you please explain how could I obtain this?

like image 929
Simon Avatar asked Jun 18 '11 16:06

Simon


People also ask

How do I add text to a figure in Matlab?

To add text to one point, specify x and y as scalars. To add text to multiple points, specify x and y as vectors with equal length. text( x , y , z , txt ) positions the text in 3-D coordinates. text(___, Name,Value ) specifies Text object properties using one or more name-value pairs.

How do you make a horizontal bar graph in Matlab?

barh( y ) creates a horizontal bar graph with one bar for each element in y . If y is an m-by-n matrix, then barh creates m groups of n bars. barh( x , y ) draws the bars along the vertical axis at the locations specified by x .

Which argument of bar () lets you thickness of the bar?

The width of the bars can be controlled by width argument of the bar() function.

What is bar plot in Matlab?

The bar function distributes bars along the x-axis. Elements in the same row of a matrix are grouped together. For example, if a matrix has five rows and three columns, then bar displays five groups of three bars along the x-axis. The first cluster of bars represents the elements in the first row of Y.


2 Answers

x = [1 2 3];
str = {'sam'; 'alan'; 'ellie'};
bar(x)
set(gca, 'XTickLabel',str, 'XTick',1:numel(str))

enter image description here

like image 151
Amro Avatar answered Nov 02 '22 15:11

Amro


http://www.mathworks.com/matlabcentral/newsreader/view_thread/21178

Labels = {'a', 'b', 'c', 'd'};
set(gca, 'XTick', 1:4, 'XTickLabel', Labels);

if you need to change the Y-labels then substitute YTickLabel for XTickLabel

like image 26
platinummonkey Avatar answered Nov 02 '22 16:11

platinummonkey