Bar Graph, Scatter Plot, Pie Chart, Histogram, Log Plot, Polar Plot

In this lesson, we will be discussing different types of graphs that MATLAB can make. I may not have covered every single type, but these are some popular ones:

1. Bar Graph

2. Scatter Plot

3. Pie Chart

4. Histogram

5. Log Plot

6. Polar Plot

video coming soon!

Bar Graph

I will use the following vectors A and B:

To create a bar graph, use the syntax bar(x,y) and replace x and y in the brackets with the variables you want to plot on the x- and y-axis respectively. It is A and B in this case, so I have bar(A,B), which gives the following graph:

Note: You could also just use bar(A) or bar(B) if you want just that one vector with its elements along the x-axis.

Now that we know how to make a basic bar graph, we can change certain aspects:

(a) Changing the style

The two main types of styles are grouped or stacked. If you have multiple y-values for each x-value, then the difference between these graphs will be obvious – otherwise it will look the same either way (eg. if you take the graph we made above and tried to change it to a stacked graph, it will look the same as a grouped graph, or vice versa).

Here are two vectors I will use to show the difference between a stacked and a grouped graph:

Now I will use bar(x,y) to create a bar graph:

This default graph is a grouped style graph. To change it to a stacked graph, I will add ‘stacked‘ at the end:

Now the grouped graph has been changed to a stacked graph:

(b) Changing the color

To change the color of the bars, indicate the color at the end of the brackets. Code for line colors was discussed in this previous lesson. For example, if I wanted to change all the bars to green from the very first graph we had made, I could use the following syntax. Create the bar graph bar(A,B) but then add in ‘g’ at the end, to change the color:

(c) Changing the width

The width of the bars can be very small (eg. 0.001) to very large (eg. 100) and even beyond that. To change the width of the bars, add in the word ‘LineWidth’ after the variables you are graphing, and then a number after that. I have added in ‘LineWidth’, 0.01:

This graph is now skinnier than the default graph would be:

Scatter Plot

I will use the following vectors A and B:

To create a scatter plot, use scatter(x,y), and replace x and y in the brackets with the variables you want to plot on the x- and y-axis respectively. These are the variables A and B here, so I will use scatter(A,B) to get the following graph:

This graph can be customized by the following changes:

(a) Change the circle size

Use the syntax scatter(x,y,circle size). It can range from very small circles (about size 10) to very large circles like I have here, with size 1000:

This is the output graph:

(b) Change the circle color

Use the syntax scatter(x,y,circle color) to change just the color, or scatter(x,y,circle size, circle color) to change both the size and the color.

Here is the syntax where I changed the size to 1000 and the color to red:

This is the output graph:

Now I have kept the same color but did not specify the size, so the graph went back to the default size:

This is the output graph:

(c) Specify marker type

If you don’t want the graph to be made out of circles, you can specify the marker type. This was also discussed in a previous lesson on Line Styles – click here if you missed that. Say I want the graph to be made of plus signs instead – I will specify the symbol for plus within the brackets (you can check out the full list of different markers in the lesson linked above). This is the syntax for changing the graph marker:

This is the output. Notice that the markers are now plus signs (+) instead of circles:

Pie Chart

A pie chart shows slices of a circle (a “pie”) for each value from a dataset. I will use the following dataset to create the pie chart:

Use the syntax pie(variable), where variable will be replaced by the variable that stores the dataset. This was variable A in this case.

Notice how the pie graph is not labelled on each slice – it’s difficult to decipher what slice corresponds to what number just by looking at it. The largest slice of the graph corresponds with the largest number in the dataset, and so on until we get to the smallest slice corresponding to the smallest number in the dataset. To put labels on the graph, we use the syntax pie(variable, labels), where you will replace the word variable with the variable that stores the dataset, and the word labels with the labels you want to add. The labels will need to match the number of slices, so each slice has a label. They need to be specified in curly brackets, in single quotes. I have specified this in the same order as the dataset A from earlier, and have created a variable called labels first:

Then you will use the syntax pie(variable, labels) I mentioned above. I replaced this with pie(A,labels), since the variable of the dataset I am using is A, and the variable with the labels I created above is called labels.

This is the output graph, with labels:

To show the individual slices of the graph, use the explode command. Create a vector explode, with the same number of values in your original dataset (I had 6 numbers in dataset A, so I had 6 numbers in this vector). You can set each number in the vector to either 0 or 1. 1 represents that the pie slice will “explode,” and 0 represents a regular pie slice. I have set these all to 1 so that every pie slice is “exploding”:

Now I will use the syntax pie(variable,explode) with variable A. This becomes:

This is the output graph. Notice how each pie slice is “exploding”:

Histogram

Histograms are a type of bar plot that group the data into bins. I will use the following set of numbers, and MATLAB will group them into bins depending on how many times each number appears:

Now to create a histogram, use the command histogram(variable), and replace the word variable with the dataset – this is variable A here.

This is the output graph:

To change the color, use the syntax histogram(variable, ‘FaceColor’, ‘color’). Replace the word variable with the variable that stores the histogram data as earlier, and the word color with the color of the graph – I will change it to green, so I am using ‘g‘. As I mentioned earlier, code for colors and line styles were specified in a previous lesson that I linked above.

This is the output graph:

Log Plot

The log plot graphs x-y points using a logarithmic scale. Aside from the difference in the axis, you could graph these plots using the plot command and it would have the same shape. Start by defining logarithmically spaced points using logspace. I have spaced the points from 1 to 5, and called this variable x:

Then I defined variable y as 3^x:

To graph these, use loglog(x,y):

This is the output graph:

Polar Plot

A polar plot is one that is in terms of r coordinates. Rather than x and y, it has theta and rho, respectively – theta is the angle in radians, rho is the radius value at each point. Here I have defined theta from 0 to 2*pi, and rho as the equation in terms of theta:

I will now plot them using the syntax polarplot(theta,rho). Your variables don’t have to be called theta and rho, so if your variable names are different, change them within the argument for polarplot.

This is the output graph. As you can see, it didn’t graph anything interesting. But notice the graph is now a circle rather than an x-y axis: