The ‘Plot’ Function

Referring back to Example 2.2 of when we imported the t_z matrix into MATLAB, what if we now wanted to plot that set of data for analysis? That can be done by using the plot(x,y) command, where x and y are one-dimensional matrices of data to be plot on the x and y axes respectively. So the data that you want to plot on the x axis has to appear as the first item in the brackets, and the y data has to appear after the comma. We will first look at how the plot function works and how to create graphs, then we’ll apply it to the t_z matrix of Example 2.2.

If you asked MATLAB to plot the point (1,2), you’d simply enter the command “plot(1,2)” into the workspace, or you would execute a script that contains that code. This will pull up a seemingly blank figure that can be adjusted to show the point. If you wanted the coordinate to show up as a point, you’d say plot(1,2,’.’). Similarly, if you wanted a cross, you’d say plot(1,2,’x’), as shown by Figure 3.1 and Figure 3.2. There is more information about line styles in the following sections.

Figure 3.1

Figure 3.1

Figure 3.2

Figure 3.2

 

We could add a few more commands to make the graphs fancier but we’ll cover that a bit later. Let’s look at another example of a plot that should be familiar, shown in Figure 3.3. The commands for it, and also their descriptors are shown in the following example, which has been extracted from http://uk.mathworks.com/help/matlab/learn_matlab/basic-plotting-functions.html

x = 0:pi/100:2*pi;

The specified range of x-values (0 to 2π with a “spacing” of π/100 )

y = sin(x);

The y-function (of x)

plot(x,y)

The command to plot y against x

hold

To hold (keep open) the graph so you can make additions to it

 

Figure 3.3

Figure 3.3

 

Note, the reason that the ‘spacing’ has been set to be so small is because MATLAB will feed the x-values into the y-function one by one, and the closer the values are to each other, the better the graphical representation. Say we used a bigger spacing of π/10 instead. Figure 3.4 shows this result, and you can see the difference especially at the peaks, which become more pointy and less curved at the sides.

Figure 3.4

Figure 3.4

 

But how significant is a graph without labels? Not very, but that’s no problem; you can add labels in MATLAB with relative ease! This will be explored in the next section.