Creating Graphs
Last updated
Was this helpful?
Last updated
Was this helpful?
In this section we will learn how to use MATLABs powerful graphics engine to create graphs that are ready for publication.
To run the plot commands in this sections, download the following dataset package which contains a few series from the FRED database (publicly available at http://fred.stlouisfed.org).
plot
commandWe will start by learning how to create line plots which are used to e.g. plot time-series data in Econometric or Macroeconomic applications.
The plot
command has three typical use cases.
plot(y)
plots variable y
against its index
plot(x, y)
plots y
against the values in x
. This requires that x
and y
have the same length.
plot(x, y, format)
is the same as above, but graphical parameters that determine the color, markers and line style of the plot are passed via format
.
Consider the following example where we plot the industrial production series from the last section.
Here 'r-.'
is the format string that tells MATLAB to use red color (r
) and to choose a dashdot line style (-.
). To see all options for the formatting strings type help plot
into the console window.
The command produces the following graph.
Plot Annotations can be used to set title, axis labels, the legend etc. In the example above you already saw some annotation commands. Here is a non-exhaustive list of useful annotation commands.
title(labelstring)
- sets the main title of the plot to the value of labelstring
.
xlabel(string)
- sets the label of the x-axis to the value of labelstring
ylabel(string)
- sets the label of the y-axis to the value of labelstring
grid
- grid on
and grid off
activate and deactivate major grid lines in the plot, respectively. grid minor
toggles additional finer grid lines.
Plot configuration commands can be used to set the scaling of axes, deactivate axis labels or to control other behaviour of the plot area. Here is a non-exhaustive list of useful configuration commands.
xlim([start end])
- sets the scale of the x-axis such that it starts at the number start
and ends at the number end
ylim([start end])
- sets the scale of the y-axis such that it starts at the number start
and ends at the number end
figure
Often we would like to create multiple plots in one script. While it is possible to execute multiple plot
commands after each other and create multiple plot windows, we only have access to the plot that was created last i.e. if we e.g. would like to use annotation or configuration commands to change the first plot we created after adding another plot this will not be possible.
The figure
command allows us to get a handle on plots so that we can access them even after running multiple plot commands.
There are two uses of the command: handlevar = figure;
creates a new figure and saves a handle to it in the variable handlevar
. figure(handlevar)
activates the figure with handle saved in handlevar
for further editing/annotation.
Here is an example.
It is a good practice to start every plot with a figure command, even if you do not wish to make changes to the plot later. That is a typical use of the plot command would look like this.
close
Sometimes we would like to close the plotting window that is currently open or close a specific plot. We can do this using the close
command. There are three different use-cases.
close
- closes the current figure window.
close(handlevar)
closes the figure whose handle was saved in handlevar
close all
- closes all plots
subplot
Often we would like to group multiple plots into one panel to be able to compare them side by side. We can achieve this by calling the subplot
command after the figure
command to arrange the plots into a matrix of plots. The subplot command has the following syntax.
subplot(m, n, p)
tells MATLAB that we would like to groupm
rows andn
columns of plots.p
is the index of the current subplot that we would like to edit wherep
is betwen 1 andm*n
(i.e. the total number of elements in the plotmatrix). Note that for subsequent calls tosubplot
within a figure,m
andn
have to match across allsubplot
commands whereasp
is changing depending on which plot we are editing.
Here is an example.
Note that when you create plot matrices with multiple rows and columns, p
is counted row-wise i.e. the first n
values of p
activate the plots along the first row, the next n
values of p
activate the plots along the second row etc.
Sometimes we would like to use other plot-types like scatter plots or bar plots. MATLAB also offers commands to create these plots. Here are a few plot types that are used in academic papers in economics. All the notation and configuration commands, as well as the figure
and subplot
commands carry over to these plot types.
scatter
commandThe scatter
command plots two variables against each other by displaying coloured circles at the locations specified by the two variables. Scatterplots thus allow to see whether there is any relationship between the two variables. The syntax of the command is the following.
scatter(x, y, s, c)
plots a scatterplot of vectorsx
andy
where both vectors need to have the same length.s
determines the size of the markers. It can either be a scalar in which case all markers have the same size or it can be a vector of the same length thanx
andy
specifying individual sizes for each marker.c
determines the colour of each marker. Whenc
is a scalar all markers have the same colour, ifc
is a vector of the same length thanx
andy
then the values of c are used to linearly interpolate the colours, taking as a basis the current colormap defined in MATLAB.
Consider the following example which creates a scatterplot of residuals from an OLS regression. In this example, the weight of 100 hospital patients was regressed on their age as well as a binary indicator for whether they smoke or not.
Note: To run the examples in this section, download the following dataset which contains data on the fitted values and residuals from the regression described above.
This procudes the following plot.
Often we would like to save our figures to the hard-drive to include them in Word or LaTeX documents that we are writing. To distribute plots for publications it is best practice to use the .pdf or .eps file format.
We can use MATLABs print
command to export plots in a variety of different formats. print(filename, formattype)
exports the current plot to the file filename
using the format specified in the format string formattype
.
The following format types are often used.
Format string
Format
Comment
'-dpdf'
Vector graphic, scales well, suitable for publications. Can be opened by most programs.
'-depsc'
.eps
Vector graphic, scales well, suitable for publications, but can´t be read by most programs. Optimized for professional printing.
'-dtiff'
.tiff
Bitmap image, does not scale well. Can be opened by most programs. Not suitable for publications.
'-djpg'
.jpg
Bitmap image, does not scale well. Most commonly used format for images. Not suitable for publications.
'-dpng'
.png
Bitmap image, does not scale well. Very small file size, suitable for webpages, blogs etc.
Here is an example.
By default the print
command will add a lot of whitespace to your plot to fill the paper layout. This is undesirable when one wants to save plots to include them in articles, books etc.
We can avoid this behaviour by reading the dimensions of the figure using the figure handle and then setting the paper size appropriately.
You can download a function here that helps you to achieve this. Unzip the folder and copy the file save_as_pdf.m
into your current working directory.
Here is an example of how to use the save_as_pdf
function.