- What is a Bar Graph in MATLAB?
- Why we use it?
- MATLAB Bar Graph Command
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.
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.
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;]
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.
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.
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”.
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’)
We can organize the bars horizontally using the command ‘barh’. Also change ‘xticks’ to ‘yticks’
yticklabels(‘21-30’, ’31-40’, ’41-50’, ’51-60’)
legend([‘before training’, ‘after training’]);
Combining a bar-graph with other MATLAB plots
Suppose we have a single data series visualized in a bar graph.
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];
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.
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’)
3D Bar Graph
3D bar graphs can be plotted using the ‘bar3’ command.
y=[3 4 5 6]
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.