March 13

Bar Graph MATLAB: Everything You Need to Know

Bar Graph MATLAB: Everything You Need to Know

Abdul Mannan

  1. What is a Bar Graph in MATLAB?
  2. Why we use it?
  3. MATLAB Bar Graph Command
    1. Bar graphs with single data series
    2. Bar graph with multiple data series
    3. Stack bars
    4. Bar color
    5. Labeling the bar-graph
    6. Horizontal Bars
    7. Combining a bar-graph with other MATLAB plots
    8. Changing the Width and Color of Bar-Graph
    9. 3D Bar Graphs
  4. Conclusion
  5. References

What is a Bar Graph in MATLAB?

Bar graph is a technique to show the serial or multiple data or percentages in the form of vertical or horizontal bar charts that levels off at the appropriate levels.

Why We Use It?

Bar graphs are widely used where we need to compare the data or to track changes over time. It is easier to understand and it gives a quick reflection of approximate results. We can use bar graphs to compare the attributes of a person or to statistically track the change over time. Take a look and decide which form is better to understand.

Fig. 1

MATLAB Bar Graph Command

Bar graphs with single data series

Start by creating one vector:


y= [5 15 25 45 10 30 60];


As you can see the values are reflecting the way we added inside our vector. Simply by calling bar method and passing the vector can create the bars along those vector points.

Fig. 2

Bar graph with multiple data series

We can simply add group of bars if we write our vector in this way.

y= [5 15 25; 4 24 34; 2 11 31;]


                                                                                 Fig. 3

We can see bars are now grouped in the form of three. Now we have seen how to create simple bar chart then we have seen group bar charts by simply adding column values in our vector. Next thing is to display stack bars.

Stack bars

Write a code:

Z = [2 2 3; 2 5 6; 2 8 9; 2 11 12]


Result will show the stack bars. Values up to 5 will be presented by first stack, higher values with another color and then the higher ones with another. That is how the groups are divided here.

Fig. 4

Bar color

We can use the bar color of our own choice. See the color code in MATLAB and follow the code here:


Here ‘r’ represents “red color”.

Fig. 5

Labeling the Bar Graph

When we use the group bar graphs, we cannot judge the exact values or vectors for which the graph is plotted. We need commands like ‘xticks’ and ‘xticlslabel’ to label x-axis of bar graph. Take another example.

y= [55 71; 42 67; 40 60; 38 57];



xticklabels(‘21-30’, ’31-40’, ’41-50’, ’51-60’)

Fig. 6

Horizontal Bars

We can organize the bars horizontally using the command ‘barh’. Also change ‘xticks’ to ‘yticks’



yticklabels(‘21-30’, ’31-40’, ’41-50’, ’51-60’)


ylabel(‘age group’);

legend([‘before training’, ‘after training’]);

Fig. 7

Combining a bar-graph with other MATLAB plots

Suppose we have a single data series visualized in a bar graph.

     Fig. 8

Now we would like to add some error bars to this graph. Suppose we have some error data that represents the uncertainty we have about these observations.

e1 = [5 8 9 10];

errorbar(1:4, y(:,1),e1,’ko’)

We can use MATLAB ‘errorbar’ command to visualize the error bars. First argument represents the ‘x’ values, second argument represents the ‘y’ values and the third argument is the error value i.e. how much up and down we go from the baseline. The ‘ko’ code represents black color with circles.

In case we want to overlay the two plots we have to add ‘hold on’ command before ‘e1’.

Now we have an error bar superposed on the bar graph.

Fig. 9

Changing the Width and Color of Bar-Graph

You can adjust the width and color of your own choice by using the following format. Size is set to 40%.

Y=[22 44 55 66]

Bar(y, .40, ‘green’)

Fig. 10

3D Bar Graph

3D bar graphs can be plotted using the ‘bar3’ command.

y=[3 4 5 6]


Fig. 11


Whenever we need to compare things, we use bar graph technique which is the easiest way to analyze the difference among the results or data. We can also use it to see the change with respect to time. We can also use them in the form of groups of two, three or whatever we want. Hence we can present our data in the most appropriate way using bar graphs in MATLAB.



Loved this? Spread the word

About the Author

Abdul Mannan is an Electrical Power Engineer with specialization in High Voltage. He's the founder and former president at Youth Entrepreneurship Society (YES), University of Engineering & Technology Taxila Campus. He is the leading contributor at "Right to Write". Connect with him about Entrepreneurship, startup ideas, creative writing, business strategies via linked in.

Abdul Mannan

Related posts

How to Use Bill of Materials (BOM) in SolidWorks: Review Beginner’s Guide

​Read More

Compare Autodesk Inventor vs Fusion 360: Review Which One to Buy

​Read More

How to Use Convert Entities Tool in SolidWorks: Beginner Tutorial

​Read More

[May 2020] Best Free CAD Design Software for Mac

​Read More
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Subscribe to our newsletter now!