MATLAB - Bar Graph



Creating bar graphs in MATLAB is a fundamental way to visualize categorical data. Bar graphs in MATLAB represent categorical data through bars of varying heights. The bar function is used to create these plots.

Syntax

bar(y)
bar(x,y)
bar(___,width)
bar(___,style)
bar(___,color)
bar(___,Name,Value)
bar(ax,___)
b = bar(___)

Let us understand the syntax in detail here −

bar(y) − The function bar(y) generates a bar chart featuring a bar for each element within the dataset y.For plotting a single set of bars, input y as a vector comprising m elements. These bars are positioned between 1 and m along the x-axis.To represent multiple sets of bars, provide y as a matrix where each column symbolizes a distinct series of data.

bar(x,y) − positions the bars precisely at the designated locations indicated by the values in x.

bar(___,width) − allows adjustment of the relative bar width, influencing the spacing between bars within a group. Define width as a single numeric value. Apply this feature in conjunction with any of the input argument combinations demonstrated earlier.

bar(___,style) − defines the appearance of bar groups. For instance, select 'stacked' to exhibit each group as a single bar with multiple colors.

bar(___,color) − gives a uniform color for all bars. For instance, designate 'r' to represent red-colored bars.

bar(___,Name,Value) − allows customization of bar graph properties using various name-value pair arguments. These are supported solely for bar graphs utilizing the default 'grouped' or 'stacked' styles.

bar(ax,___) − the function plots on the axes defined by ax rather than the current axes (gca). The ax option can come before any of the input argument combinations demonstrated earlier.

b = bar(___) − generates one or more Bar objects. When y is a vector, a single Bar object is created. In the case of y being a matrix, bar produces a Bar object for each series. Utilize b to modify bar properties once the bar graph is displayed.

Create Bar Graph

Example 1

Here let us create a simple bar graph as shown in the example below−

y = [50, 85, 110, 135, 155, 180, 210, 230, 255, 280, 300];
bar(y)

Now when you execute the same in the matlab command window the output is −

bar graph

Example 2

Here we are going to make use of x and y vectors to create a bar graph as shown below −

x = 1900:10:2000;
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(x,y)

This code will generate a bar graph with bars representing population growth over decades, where the x-axis denotes the years and the y-axis shows the population in millions.

The output on execution in matlab command window is as follows −

matlab command window

Example 3

This example shows how to use width on the bar graph Will make use of the same values for x and y as used in the example above.

x = 1900:10:2000;
y = [75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(x,y, 0.2)

On execution in matlab the output is −

matlab execution

Example 4 : Groups of bars together

In this example will plot 5 groups of three bars along the x-axis.

Y = [5,2,1
     8,7,3
     9,8,6
     5,5,5
     4,3,2];

bar(Y)

On execution the output is −

bars together

Example 5 : Stacked Bars.

Stacking the bar together, and displaying one bar with the height of the bar equal to the sum of rows.

Y = [5,2,1
     8,7,3
     9,8,6
     5,5,5
     4,3,2];

bar(Y,'stacked')

The output on execution is −

stacked bars

Example 6 : Displaying negative data on bar graphs.

Y = [5,-2,1
   8,7,3
   -9,8,6
   5,5,-5
   -4,3,22];

bar(Y,stacked)

The output on execution is −

negative data

Example 7: Giving color to the bar graph

In the example below will give a green color to the bar graph as shown below −

y = [50, 85, 110, 135, 155, 180, 210, 230, 255, 280, 300];
bar(y,'g')

The code on execution on matlab command window is as follows −

color to bar graph

Example 8 : To give interior and outline color to the bar graph

The example below will give an outline and interior color using RGB.

y = [50, 85, 110, 135, 155, 180, 210, 230, 255, 280, 300];
bar(y,'FaceColor',[0 .3 .2],'EdgeColor',[0 .7 .7],'LineWidth',1.8)

On execution in matlab command window the output is −

interior and outline

Example 9 : Bar Chart with Colormap Colors

Generate a bar graph employing colormap colors by configuring the FaceColor property to 'flat'. Afterwards, assign an integer to the CData property for each Bar object.

The code is as follows −

A = [6 9 12; 13 45 7; 83 44 12];
b = bar(A,'FaceColor','flat');
for k = 1:size(A,2)
   b(k).CData = k;
end

After execution in matlab the output is −

colormap colors
Advertisements