Lesson 3.1

Plots and Figures

Summary

  • Plot vector data in 2D.
  • Learn the difference between a figure and a plot in MATLAB.
  • Create plots of functions.
  • Use multiple figures.
  • Combine multiple plots on the same figure.

Plotting in MATLAB

Plotting in MATLAB is simple, which is one advantage it has over some other programming languages like C++ or C#. To create a plot in MATLAB, you just need your data in vector form (1D matrix) and that is it. To create a 2D plot, we can use the plot() function, as seen in Example 1. The independent (horizontal) and dependent (vertical) vectors must have the same number of data points in them.

Example 1: Entering Simple Matrices and Vectors
Editor
%Creating some arbitrary data to plot. %As usual, variable names do not matter: You do not have to call them "x" and "y". x = [1 5 6 6.3 32 43]; %Defining the domain of the data (independent variable) y = [1 25 36 39.7 1024 1849]; %Defining the dependent variable data figure %Creating a blank figure window for MATLAB to plot in plot(x,y) %Adding a plot to the figure showing the data [Try this code yourself with Octave Online! Click Here]

Figure 1: Figure output from Example 1. The independent variable, x, is on the horizontal axis.

What Kind of Plots Can MATLAB Create?

MATLAB can do almost any type of plot you can think of! 2D and 3D plots, radar/polar plots, bar graphs, scatter plots, heat maps, and the list goes on. In this lesson, we will focus on only 2D plots. However, in the lesson on advanced plotting (LINK TO LESSON), we will cover some other types of plotting.

What Is the Difference Between a Figure and a Plot?

A figure is the window containing a plot. A plot is the specific graphic that is displayed in the figure. You can see they are called separately in Example 1 where we first open a figure and then print a plot to it. Therefore, a figure can exist without a plot, but not vice versa as you can see in Figure 2.


Figure 2: A blank MATLAB figure.

Plotting Functions in MATLAB

Now that we know how to plot a set of vectors, how do we plot a function like y(x)=x2? We know that we must have the data in vector form to plot it (the same is essential true of graphing on paper - you have to plug in sets of values to plot). So the task is simply to create a vector of numbers corresponding to the function’s value for different inputs; e.g., y(1)=1, y(2)=4, y(6)=36.

Example 2: Plotting a Function in MATLAB
Editor
%Creating some arbitrary data to plot. %As usual, variable names do not matter: you do not have to %call them "x" and "y". x = [1 5 6 6.3 32 43]; %Defining the domain of the data (independent variable) y = x.^2 %Although we have the same data as in Example 1, we use the underlying % function, x^2, to find the dependent variable vector. figure %Creating a figure window for MATLAB to plot in plot(x,y) %Adding a plot to the figure showing the data Command Window Output
y = 1 25 36 39.7 1024 1849 [Try this code yourself with Octave Online! Click Here]

Figure 3: Figure output from Example 2. The independent variable, x, is on the horizontal axis, while the dependent variable, y, is on the vertical axis.

Create Multiple Figures

Often, we will want to create multiple figures (in different windows). To create a new figure window to plot in, simply call the figure command again as done below in Example 3.

Important Note: Variables for plotting can be any variable name: not just “x” and “y”.

Example 3: Plotting a Function in MATLAB
Editor
figure %Creating a figure window for MATLAB to plot in x = [1 5 6 6.3 32 43]; %Defining the domain of the data (independent variable) y = x.^2; %Defining the corresponding dependent data using the x vector plot(x,y) %Plotting the data set figure %Creating a second window for MATLAB to plot in t = 1 : 0.1 :43; %Defining a second domain of the data (independent variable) % A vector with many data points will look smoother. % We define vector "t" over a domain with a beginning, interval, and end. p = 2*t.^2; %Creating some other, second dependent variable vector using the t vector. plot(t,p,'r') %Plotting the second set of data. %Note: The "r" in this plot function makes the line color red. [Try this code yourself with Octave Online! Click Here]


Figure 4: Figure outputs from Example 3.

Combining Multiple Plots on the Same Figure

Instead of creating two different figures with separate plots (like in Example 3), we may want to combine these plots on the same figure. To accomplish this, we can use the hold on command, which tells MATLAB to place subsequent plots on the current figure and not to replace the current figure contents.

Example 4: Combining Multiple Plots
Editor
figure %Creating a figure window for MATLAB to plot in x = [1 5 6 6.3 32 43]; %Defining the domain of the data (independent variable) y = x.^2; %Defining the corresponding dependent data (dependent variable) plot(x,y) %Plotting the data %Telling MATLAB to place new plots on the same figure rather than overwriting the figure contents hold on t = 1 : 0.1 :43; %Defining a second domain of the data (independent variable) %We define vector 't' over a domain with a beginning, interval, and end. p = 2*t.^2; %Creating some other dependent variable vector from the t variable. plot(t,p,'r') %Plotting the second set of data. %Note: The 'r' in this plot command makes the line color red. %Command ”hold off” is the reverse of “hold on”. %Any plotting after “hold off” will overwrite the current figure contents. hold off %NOTE: Without 'hold on', MATLAB would plot over the first plot call with x and y. % Try it yourself %by removing 'hold on'. [Try this code yourself with Octave Online! Click Here]

Figure 5: Figure output from Example 4. Two data sets plotted on the same figure.

After telling MATLAB hold on, it locks current axes formatting. This is especially important when plotting things outside the default axes: for example, logarithmic plots.

Plotting with Nonlinear Axes (Log Plots)

Logarithmic plots use one or more logarithmic (nonlinear) axes to display and scale the data. Logarithmic plots can be created using the semilogy() command. Note, there are also loglog(), both axes are logarithmic, and semilogx(), x axis is logarithmic. Example 5 demonstrates how to properly combine two semilogy() plots using hold on (see the resulting plot in Figure 6).

If hold on is incorrectly placed before a semilogy() plot, MATLAB will lock the plot to default axes, which are the at a normal (linear) scale, instead of changing to a log (nonlinear) scale. This applies to all plots that do not have default axes settings on both x and y axes (not just logarithmic plots). To be safe, always place hold on after the first plot as we have done in Examples 4 & 5.

Important Note: Right after telling MATLAB hold on, it locks current axis formatting.

Example 5: Combining Multiple Logarithmic Plots
Editor
figure %Creating a window for MATLAB to plot in %Creating some arbitrary data to plot. x = [1 5 6 6.3 32 43]; y = log(x); %Remember, log() takes the log of each element when % given an array and returns the resulting array. semilogy(x,y) %Plotting the data with a logarithmic y axis and linear x axis hold on %Telling MATLAB to place new plots on the same figure rather than % overwriting the figure contents. t = x(1):0.1:x(end); %Defining the independent variable vector with many data % points for smoothness. We define 't' over the domain (start to end) of 'x'. h = log(4*t); % Defining a logarithmic dependent variable semilogy(t,h,'g') %Plotting the second set of data on the same figure and axes. % The 'g' makes the color of the line green. %Note the placement of “hold on” [Try this code yourself with Octave Online! Click Here]

Figure 6: Figure output from Example 5. The dependent/vertical axis is on a logarithmic scale.

Important Note: When combining plots on the same figure, they must be of the same type. That is, you cannot combine a linear scale and logarithmic scale plot on the same figure.

Next Lesson

In the next lesson, we will discover how to add and edit plot formatting.