Bar Graph in MATLAB
Last Updated :
29 Feb, 2024
A Bar Graph is a diagrammatic representation of non-continuous or discrete variables. It is of 2 types vertical and horizontal. When the height axis is on the y-axis then it is a vertical Bar Graph and when the height axis is on the x-axis then it is a horizontal Bar Graph.
In MATLAB we have a function named bar() which allows us to plot a bar graph.Â
Syntax:
bar(X,Y) where X and Y represent the x and the y axis of the plane. The X and Y both are vectors.
Now let’s move to some examples.
Example 1: A simple Bar graph:
MATLAB
x=100:20:160;
y=[22 44 55 66];
bar(x,y,0.6, "green" );
|
Output :Â
Example 2: 3 groups of 4 bars:
MATLAB
y=[2 5 4 1; 5 3 3 1; 2 8 4 6];
bar(y);
|
Output :Â
Example 3: Display Stacked bars:
MATLAB
y=[2 5 4 1; 5 3 3 1; 2 8 4 6];
bar(y, 'stacked' );
|
Output : Â
Example 4: Display negative bars:
MATLAB
y=[2 5 4 -1; 5 -3 3 1; -2 8 4 6];
bar(y);
|
Output :
Example 5: Display horizontal bar graph:
Output : Â
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...