Line Plots in MATLAB

Tutorial Video

Introduction

This will take you through the basics of performing a 2D Plot in MATLAB.

The figure Function

The function

<span style="color: #0000ff;">figure</span>

Will create a new figure using the lowest unused positive integer. e.g. if no additional windows were previously open it will create Figure 1.

If an input argument is selected. e.g.

<span style="color: #0000ff;">figure(</span>100<span style="color: #0000ff;">)</span>

MATLAB will instead create Figure 100.

If an input argument is selected of a figure that exists. e.g.

<span style="color: #0000ff;">figure(</span>1<span style="color: #0000ff;">)</span>

Then MATLAB will reselect Figure 1. Any sub-sequential commands will apply to that Figure.

Getting the Data From Excel

Copying Each Column to a New Variable

We have the data we want to plot in an Excel Spreadsheet. Although it is pretty easy to create Variables by typing it in, in MATLAB we will create a new variable call it v and copy the first column to it.

We will then do the same with the second column but this time calling it v:

We can dock Figure(1) by pressing the Dock Button:

I want it snapped to the right of Variables, not below them so I will drag it to the right:

Now the figure is docked:

Since I have x and y data in the form of t and v I can use the plot command:

<span style="color: #0000ff;">plot(</span>t<span style="color: #808080;">,</span>v<span style="color: #0000ff;">)</span>

We can also select the Columns we want to plot from a Variable that is a Matrix or alternatively if or input data is in the form of a Table we can select the Table Variables (Columns) to Plot.

Copying All Columns to a New Variable and Selecting which Columns to Plot

If instead of copying the Excel Data to separate Variables t and v, it was copied over as a single Variable vt:

The Command

<span style="color: #0000ff;">plot(</span>tv<span style="color: #0000ff;">)</span>

Would give:

Note the previous plot in Figure 1 is overridden because Figure 1 is still selected.

In this dataset, no x values were selected so the row index was used for the x values and t and v were plotted with respect to this index.

This isn't what we wanted, we wanted to plot the first column of vt which is t with respect to the second column of vt which is v. We can explicitly state this in the command:

<span style="color: #0000ff;">plot(</span><span style="color: #00ccff;">tv(</span><span style="color: #ff0000;">:</span><span style="color: #808080;">,</span>1<span style="color: #00ccff;">)</span><span style="color: #808080;">,</span><span style="color: #00ccff;">tv(</span><span style="color: #ff0000;">:</span><span style="color: #808080;">,</span>2<span style="color: #00ccff;">)</span><span style="color: #0000ff;">)</span>

Making a Table from

For more practice we can also create a table from the columns v and t and plot by selecting the table variables

tabletv=<span style="color: #0000ff;">table(</span>t<span style="color: #808080;">,</span>v<span style="color: #0000ff;">)</span>

<span style="color: #0000ff;">plot(</span>tabletv<span style="color: #ff0000;">.</span>t<span style="color: #808080;">,</span>table2<span style="color: #ff0000;">.</span>v<span style="color: #0000ff;">)</span>

tabletv2=<span style="color: #0000ff;">table(<span style="color: #00ccff;">tv(</span><span style="color: #ff0000;">:</span><span style="color: #808080;">,</span><span style="color: #000000;">1</span><span style="color: #00ccff;">)</span><span style="color: #808080;">,</span><span style="color: #00ccff;">tv(</span><span style="color: #ff0000;">:</span><span style="color: #808080;">,</span><span style="color: #000000;">2</span><span style="color: #00ccff;">)</span>)</span>
tabletv2.Properties.VariableNames={'t','v'}
plot(tv2(:,1),tv2(:,2))

Additional Input Arguments

For the Plot Function, many of the additional input arguments come in pairs. The first value selects the setting to be changed and the second value selects the value for such a setting to be changed to. Unlike the names of functions or variables when the inputs are characters, they are not case sensitive.

LineWidth

These input arguments specify the thickness of the line. The first value is a string of characters and the second value is a scalar.

<span style="color: #3366ff;">plot(</span>t<span style="color: #808080;">,</span>v<span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;"><span style="color: #808080;">,</span>5</span></span><span style="color: #3366ff;">)</span>

Color

This is spelt using English (U.S.). The English (U.K.) spelling will not work.

These input arguments specify the colour of the line. The first value is a string of characters and the second value for primary and secondary colours can either be a string of characters as the full colour name or it's abbreviation. Alternatively it can be specified as a

[R,G,B]

value. For this the RGB values are normalised between 0 and 1. In many other applications they range between 0 and 255.

<span style="color: #3366ff;">plot(</span>t<span style="color: #808080;">,</span>v<span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'<span style="color: #000000;"><span style="color: #808080;">,</span><span style="color: #800080;">'</span>Red<span style="color: #800080;">'</span></span></span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>t<span style="color: #808080;">,</span>v<span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'<span style="color: #000000;"><span style="color: #808080;">,</span>'R'</span></span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>t<span style="color: #808080;">,</span>v<span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'<span style="color: #000000;"><span style="color: #808080;">,</span>[<span style="color: #ff0000;">1</span>,<span style="color: #00ff00;">0</span>,<span style="color: #0000ff;">0</span>]</span></span><span style="color: #3366ff;">)</span>

All colours are made from a mixture of three primary colours:

  • red
  • green
  • blue

Black is shown in the absence of these three colours and white is shown when these three colours are at full brightness.

Full NameAbbreviationRGB Value 
'Red''R'[1,0,0]
'Green''G'[0,1,0]
'Blue''B'[0,0,1]
'Magenta''M'[1,0,1]
'Yellow''Y'[1,1,0]
'Cyan''C'[0,1,1]
'Black''K'[0,0,0]
'White''W'[1,1,1]

As seen a colour can be expressed as a 

[r,g,b]

 row vector opposed to a pre-existing string.

Many programs such as Microsoft Office list colors in 8 bit, which means they list 2^8=256 levels. If we subtract 1 from 256 to get a 0 value with range from 0-255. We may use the colour pickers in Microsoft Word to determine the colour we want to use. Select the button to change the colour of the text, select More Colours:

Select your colour and then select custom:

You now get your RGB values listed from 0 to 255.

We can create a row vector to it and assign it to a variable name. For instance using the three numbers shown above and dividing them through by 255.

mw_wine=[192/255,0/255,0/255]

Repeating this with the standard colours in Microsoft Word we get:

Custom Variable NameRGB Value
mw_wine[192/255,0/255,0/255]
mw_red[255/255,0/255,0/255]
mw_orange[255/255,192/255,0/255]
mw_yellow[255/255,192/255,11/255]
mw_lime[146/255,208/255,80/255]
mw_green[0/255,176/255,80/255]
mw_sky[3/255,177/255,240/255]
mw_blue[59/255,145/255,207/255]
mw_navy[0/255,32/255,96/255]
mw_purple[114/255,51/255,161/255]
mw_grey[116/255,116/255,116/255]

It is often useful to put these custom colours in a script so you can call this script before plotting to get your desired colour choice.

<span style="color: #3366ff;">plot(</span>t<span style="color: #808080;">,</span>v<span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'<span style="color: #000000;"><span style="color: #808080;">,<span style="color: #000000;">[</span><span style="color: #ff0000;">255/255</span>,<span style="color: #00ff00;">192/255</span>,<span style="color: #0000ff;">0/255</span><span style="color: #000000;">]</span></span></span></span><span style="color: #3366ff;">)</span>

Now we can use the Variable Name in place of the RGB row vector:

customcolours
<span style="color: #3366ff;">plot(</span>t<span style="color: #808080;">,</span>v<span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'<span style="color: #000000;"><span style="color: #808080;">,</span><span style="color: #ff0000;">mw_orange</span></span></span><span style="color: #3366ff;">)</span>

Because we are using a Variable Name opposed to a Character there is no

<span style="color: #800080;">' </span><span style="color: #800080;">'</span>

.

LineWidth

As these input arguments come in pairs. It doesn't matter if we specify the Color first or the LineWidth first:

<span style="color: #3366ff;">plot(</span>t<span style="color: #808080;">,</span>v<span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'<span style="color: #000000;"><span style="color: #808080;">,[<span style="color: #ff0000;">255/255</span>,<span style="color: #00ff00;">192/255</span>,<span style="color: #0000ff;">0/255</span>]</span><span style="color: #ff0000;"><span style="color: #808080;">,</span>'<span style="color: #ff9900;">LineWidth</span>'<span style="color: #808080;">,</span>5</span></span></span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>t<span style="color: #808080;">,</span>v<span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;"><span style="color: #808080;">,</span>5<span style="color: #808080;">,</span>'<span style="color: #ff9900;">Color</span>'<span style="color: #808080;">,[<span style="color: #ff0000;">255/255</span>,<span style="color: #00ff00;">192/255</span>,<span style="color: #0000ff;">0/255</span>]</span></span></span><span style="color: #3366ff;">)</span>

It can also be helpful using 

<span style="color: #ff00ff;">...</span>

to split the code over multiple lines. That way you can easily track the pairs:

<span style="color: #3366ff;">plot(</span>t<span style="color: #808080;">,</span>v<span style="color: #808080;">,<span style="color: #ff00ff;">...</span>
</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'<span style="color: #000000;"><span style="color: #808080;">,[<span style="color: #ff0000;">255/255</span>,<span style="color: #00ff00;">192/255</span>,<span style="color: #0000ff;">0/255</span>]</span><span style="color: #ff0000;"><span style="color: #808080;">,<span style="color: #ff00ff;">...</span>
</span>'<span style="color: #ff9900;">LineWidth</span>'<span style="color: #808080;">,</span>5</span></span></span><span style="color: #3366ff;">)</span>

The lines of code above give:

LineStyle

MATLAB also has a number of different types of lines, by default the line is a solid line however another two input arguments can be input to change these:

\displaystyle \begin{array}{*{20}{c}} {\text{Line Style}} & {\text{Specifier}} \\ {\text{solid line}} & \text{-} \\ {\text{dashed line}} & {\text{--}} \\ {\text{dotted line}} & \text{:} \\ {\text{dash-dot line}} & {\text{- }\text{.}} \end{array}

Once again

<span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>

and

<span style="color: #800080;">'</span><span style="color: #ff9900;">--</span><span style="color: #800080;">'</span>

are both strings of characters and must be enclosed in

<span style="color: #800080;">'</span>  <span style="color: #800080;">'</span>
<span style="color: #3366ff;">plot(</span>t,v,...
<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,<span style="color: #800080;"><span style="color: #000000;"><span style="color: #808080;">[<span style="color: #ff0000;">255/255</span>,<span style="color: #00ff00;">192/255</span>,<span style="color: #0000ff;">0/255</span>]</span></span></span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">--</span><span style="color: #800080;">'</span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5</span></span><span style="color: #3366ff;">)</span>

LineStyle 
'-'
'--'
':'
'-.'

Marker

Markers can also be added to the line using two more additional input arguments:

<span style="color: #3366ff;">plot(</span>t,v,...
<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,<span style="color: #800080;"><span style="color: #000000;"><span style="color: #808080;">[<span style="color: #ff0000;">255/255</span>,<span style="color: #00ff00;">192/255</span>,<span style="color: #0000ff;">0/255</span>]</span></span></span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'<span style="color: #ff9900;">:</span></span><span style="color: #800080;">'</span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5,<span style="color: #ff00ff;">...</span>
<span style="color: #993366;">'</span><span style="color: #ff9900;">Marker</span><span style="color: #993366;">'</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">s</span><span style="color: #993366;">'</span></span></span><span style="color: #3366ff;">)</span>

Once again

<span style="color: #800080;">'</span><span style="color: #ff9900;">Marker</span><span style="color: #800080;">'</span>

and

<span style="color: #800080;">'</span><span style="color: #ff9900;">s</span><span style="color: #800080;">'</span>

are both strings of characters and must be enclosed in

<span style="color: #800080;">'</span>  <span style="color: #800080;">'</span>

MarkerStyle 
'+'
'o'
'*'
'.'
'x'
's'
'd'
'^'
'<'
'>'
'v'
'p'
'h'

MarkerSize

The markers above were quite small, they are hard to read, this can be resolved by using another two additional input arguments:

<span style="color: #3366ff;">plot(</span>t,v,...
<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,<span style="color: #800080;"><span style="color: #000000;"><span style="color: #808080;">[<span style="color: #ff0000;">255/255</span>,<span style="color: #00ff00;">192/255</span>,<span style="color: #0000ff;">0/255</span>]</span></span></span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'<span style="color: #ff9900;">:</span></span><span style="color: #800080;">'</span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5,<span style="color: #ff00ff;">...</span>
<span style="color: #993366;">'</span><span style="color: #ff9900;">Marker</span><span style="color: #993366;">'</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">s</span><span style="color: #993366;">'<span style="color: #000000;">,</span><span style="color: #ff00ff;">...</span>
'<span style="color: #ff9900;">MarkerSize</span>'<span style="color: #000000;">,20</span></span></span></span><span style="color: #3366ff;">)</span>

Once again

<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerSize</span><span style="color: #800080;">'</span>

 is a string and must be enclosed in

<span style="color: #800080;">'</span>  <span style="color: #800080;">'</span>

 and 20 is a scalar.

MarkerEdge and MarkerFace

The Marker does not need to be the same colour as the line. We can specify both the Edge and the Fill of the marker with another colour using another four input arguments. Once again

<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFace</span><span style="color: #800080;">'</span>

and

<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdge</span><span style="color: #800080;">'</span>

are both strings and must be enclosed in

<span style="color: #800080;">'</span>  <span style="color: #800080;">'</span>

. Each of them has a paired input argument, which uses the same notation as the line color (see above), I will use the 

[<span style="color: #ff0000;">r</span>,<span style="color: #00ff00;">g</span>,<span style="color: #0000ff;">b</span>]

row vector notation.

<span style="color: #3366ff;">plot(</span>t,v,...
<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,<span style="color: #800080;"><span style="color: #000000;"><span style="color: #808080;">[<span style="color: #ff0000;">255/255</span>,<span style="color: #00ff00;">192/255</span>,<span style="color: #0000ff;">0/255</span>]</span></span></span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'<span style="color: #ff9900;">:</span></span><span style="color: #800080;">'</span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5,<span style="color: #ff00ff;">...</span>
<span style="color: #993366;">'</span><span style="color: #ff9900;">Marker</span><span style="color: #993366;">'</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">s</span><span style="color: #993366;">'<span style="color: #000000;">,</span><span style="color: #ff00ff;">...</span>
'<span style="color: #ff9900;">MarkerSize</span>'<span style="color: #000000;">,20,<span style="color: #ff00ff;">...</span>
</span></span></span></span><span style="color: #3366ff;"><span style="color: #800080;"><span style="color: #000000;"><span style="color: #993366;">'<span style="color: #ff9900;">MarkerEdge</span>'<span style="color: #000000;">,[</span><span style="color: #ff0000;">0/255</span>,<span style="color: #00ff00;">176/255</span>,<span style="color: #0000ff;">80/255</span><span style="color: #000000;">],</span><span style="color: #ff00ff;">...
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFace</span><span style="color: #800080;">'</span> <span style="color: #000000;">,[</span><span style="color: #ff0000;">112/255</span><span style="color: #000000;">,</span><span style="color: #00ff00;">48/255</span><span style="color: #000000;">,</span><span style="color: #0000ff;">160/255</span><span style="color: #000000;">]</span></span></span></span></span>)</span>

Short Hand

For learning pairing the inputs may help for using the plot function but it is quite useful to use the abbreviated notation for some of the most commonly used inputs.

<span style="color: #3366ff;">plot(</span>t,v, <span style="color: #800080;">'</span><span style="color: #ff0000;">color</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #00ffff;">b</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff0000;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #00ffff;">:</span><span style="color: #800080;">'</span>,'<span style="color: #ff0000;">Marker</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #00ffff;">s</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span>t,v,<span style="color: #800080;">'</span><span style="color: #00ffff;">b:s</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

Axes

For the axes we use the function

<span style="color: #0000ff;">set</span>

with the input argument

gca

(get current axes):

<span style="color: #0000ff;">set(</span>gca<span style="color: #0000ff;">)</span>

Limits

To change the x axis limits the additional input argument 

<span style="color: #800080;">'</span><span style="color: #ff9900;">XLim</span><span style="color: #800080;">'</span>

 in the for of a string  of characters is matched by a 2 by 1 row vector consisting of the upper and lower x limits respectively. Likewise we have the additional input argument 

<span style="color: #800080;">'</span><span style="color: #ff9900;">YLim</span><span style="color: #800080;">'</span>

 matched input argument is a 2 by 1 column vector (if a row vector is input, it will automatically be adjusted) this time consisting of the upper and lower y limits respectively.

<span style="color: #0000ff;">set(</span>gca,<span style="color: #ff00ff;">...<span style="color: #000000;">,</span></span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">XLim</span><span style="color: #800080;">'</span>,[10,20],<span style="color: #ff00ff;">...</span>
<span style="color: #0000ff;"><span style="color: #800080;">'</span><span style="color: #ff9900;">YLim</span><span style="color: #800080;">'</span><span style="color: #000000;">,[400,500]</span>)</span>

Scale

We can also use the additional input argument 

<span style="color: #800080;">'</span><span style="color: #ff9900;">XLim</span><span style="color: #800080;">'</span>

  to set the scale on the x axis using the accompanying input argument 

<span style="color: #0000ff;"><span style="color: #800080;">'</span><span style="color: #ff9900;">Lin</span><span style="color: #800080;">'</span></span>

for Linear (default) or 

<span style="color: #0000ff;"><span style="color: #800080;">'</span><span style="color: #ff9900;">Log</span><span style="color: #800080;">'</span></span>

for logarithmic scale. For the y scale we use 

<span style="color: #800080;">'</span><span style="color: #ff9900;">YLim</span><span style="color: #800080;">'</span>
<span style="color: #0000ff;">set(</span>gca,<span style="color: #ff00ff;">...<span style="color: #000000;">,</span></span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">XLim</span><span style="color: #800080;">'</span>,[10,20],<span style="color: #ff00ff;">...</span>
<span style="color: #0000ff;"><span style="color: #800080;">'</span><span style="color: #ff9900;">YLim</span><span style="color: #800080;">'</span><span style="color: #000000;">,[100,500],<span style="color: #ff00ff;">...</span>
</span><span style="color: #800080;">'<span style="color: #ff9900;">X</span></span><span style="color: #ff9900;">Scale</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">Lin</span><span style="color: #800080;">'<span style="color: #000000;">,<span style="color: #ff00ff;">...</span></span></span>
<span style="color: #800080;">'<span style="color: #ff9900;">Y</span></span><span style="color: #ff9900;">Scale</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">Log</span><span style="color: #800080;">'</span>)</span>

FontSize

If we wish to change the Font Size on the axes we can use the additional input argument 

<span style="color: #0000ff;"><span style="color: #800080;">'<span style="color: #ff9900;">FontSize</span>'</span></span>

followed by a scalar input:

<span style="color: #0000ff;">set(</span>gca,<span style="color: #ff00ff;">...<span style="color: #000000;">,</span></span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">XLim</span><span style="color: #800080;">'</span>,[10,20],<span style="color: #ff00ff;">...</span>
<span style="color: #0000ff;"><span style="color: #800080;">'</span><span style="color: #ff9900;">YLim</span><span style="color: #800080;">'</span><span style="color: #000000;">,[100,500],<span style="color: #ff00ff;">...</span>
</span><span style="color: #800080;">'<span style="color: #ff9900;">X</span></span><span style="color: #ff9900;">Scale</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">Lin</span><span style="color: #800080;">'<span style="color: #000000;">,<span style="color: #ff00ff;">...</span></span></span>
<span style="color: #800080;">'<span style="color: #ff9900;">Y</span></span><span style="color: #ff9900;">Scale</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">Log</span><span style="color: #800080;">'<span style="color: #000000;">,</span><span style="color: #ff00ff;">...</span>
'<span style="color: #ff9900;">FontSize</span>'<span style="color: #000000;">,20</span></span>)</span>

Grid

One can enable the grid by using the function

<span style="color: #0000ff;">grid</span>

with the input argument

<span style="color: #800080;">'</span><span style="color: #ffcc00;">on</span><span style="color: #800080;">'</span>
<span style="color: #3366ff;">grid(<span style="color: #ffcc00;"><span style="color: #800080;">'</span>on<span style="color: #800080;">'</span></span>)</span>

We can have gridlines with minor gridlines using the input argument

<span style="color: #800080;">'</span><span style="color: #ffcc00;">minor</span><span style="color: #800080;">'</span>
<span style="color: #3366ff;">grid(<span style="color: #ffcc00;"><span style="color: #800080;">'</span>minor<span style="color: #800080;">'</span></span>)</span>

One can disable the grid by using the function

<span style="color: #0000ff;">grid</span>

with the input argument

<span style="color: #800080;">'</span><span style="color: #ffcc00;">off</span><span style="color: #800080;">'</span>
<span style="color: #3366ff;">grid(<span style="color: #ffcc00;"><span style="color: #800080;">'</span>off<span style="color: #800080;">'</span></span>)</span>

Because these are commonly used, they also in the form:

<span style="color: #3366ff;">grid <span style="color: #000000;">on</span></span>
<span style="color: #3366ff;">grid <span style="color: #000000;">minor</span></span>
<span style="color: #3366ff;">grid <span style="color: #000000;">off</span></span>

Axis Labels and Title

There are three functions to label the x-axis, y-axis and the figure title these are:

<span style="color: #3366ff;">xlabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">time (s)</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">ylabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">speed (m/s)</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">title(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Rocket Projectile</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

Note how the brackets enclosed within the 

<span style="color: #800080;">'</span>  <span style="color: #800080;">'</span>

 are treated as text and not as part of the code.

If alternatively one has a cell array with the labels, these can also be input as axis labels:

<span style="color: #800080;"><span style="color: #ff0000;"><span style="color: #000000;">mytestlabels=</span><span style="color: #666699;">{</span></span>'<span style="color: #ff9900;">test</span> </span><span style="color: #ff9900;">time (s)</span><span style="color: #800080;"><span style="color: #800080;">'</span><span style="color: #000000;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">test</span><span style="color: #800080;"> </span><span style="color: #ff9900;">speed (m/s)</span><span style="color: #800080;">'</span><span style="color: #000000;">,'</span><span style="color: #000000;"><span style="color: #ff9900;">Rocket Projectile</span><span style="color: #000000;">'</span><span style="color: #ff0000;"><span style="color: #666699;">}</span></span></span></span>
<span style="color: #3366ff;">xlabel(<span style="color: #33cccc;">mytestlabels(<span style="color: #000000;">1</span>)</span>)</span>
<span style="color: #3366ff;">ylabel(<span style="color: #00ccff;">mytestlabels(<span style="color: #000000;">2</span>)</span>)</span>
<span style="color: #3366ff;">title(<span style="color: #00ccff;">mytestlabels(<span style="color: #000000;">3</span>)</span>)</span>

Legend

The graph is missing a legend we can add one using the following function

<span style="color: #3366ff;">legend(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Rocket1</span><span style="color: #800080;">'</span><span style="color: #ff00ff;"><span style="color: #000000;">,</span>...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">Location</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">NorthWest</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

<span style="color: #3366ff;">legend(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Rocket1</span><span style="color: #800080;">'</span><span style="color: #ff00ff;"><span style="color: #000000;">,<span style="color: #ff00ff;">...</span></span></span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">Location</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">SouthEast</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

For the location we use the compass points, these are usually within the chart area but we can also specify for them to be outside.

Location
'North'
'NorthEast'
'East'
'SouthEast'
'South'
'SouthWest'
'West'
'NorthWest'
'West'
'Best'
'NorthOutside'
'EastOutside'
'SouthOutside'
'WestOutside'
'BestOutside'

Hold

So far we have the following code.

<span style="color: #0000ff;">figure(</span>1<span style="color: #0000ff;">)</span>
<span style="color: #3366ff;">plot(</span>t,v,...
<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span><span style="color: #000000;">,</span><span style="color: #800080;"><span style="color: #000000;"><span style="color: #808080;"><span style="color: #000000;">[</span><span style="color: #ff0000;">255/255</span><span style="color: #000000;">,</span><span style="color: #00ff00;">192/255</span><span style="color: #000000;">,</span><span style="color: #0000ff;">0/255</span><span style="color: #000000;">]</span></span></span></span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'<span style="color: #ff9900;">:</span></span><span style="color: #800080;">'</span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5,<span style="color: #ff00ff;">...</span>
<span style="color: #993366;">'</span><span style="color: #ff9900;">Marker</span><span style="color: #993366;">'</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">s</span><span style="color: #993366;">',<span style="color: #ff00ff;">...</span>
'<span style="color: #ff9900;">MarkerSize</span>',20,<span style="color: #ff00ff;">...</span>
</span></span></span><span style="color: #3366ff;"><span style="color: #800080;"><span style="color: #000000;"><span style="color: #993366;">'<span style="color: #ff9900;">MarkerEdge</span>',[<span style="color: #ff0000;">0/255</span>,<span style="color: #00ff00;">176/255</span>,<span style="color: #0000ff;">80/255</span>],<span style="color: #ff00ff;">...
'<span style="color: #ff9900;">MarkerFace</span>' ,[<span style="color: #ff0000;">112/255</span>,<span style="color: #00ff00;">48/255</span>,<span style="color: #0000ff;">160/255</span>]</span></span></span></span>)</span>
<span style="color: #0000ff;">set(</span>gca,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">XLim</span><span style="color: #800080;">'</span>,[10,20],<span style="color: #ff00ff;">...</span>
<span style="color: #0000ff;"><span style="color: #800080;">'</span><span style="color: #ff9900;">YLim</span><span style="color: #800080;">'</span><span style="color: #000000;">,[100,500],<span style="color: #ff00ff;">...</span></span><span style="color: #800080;">
'<span style="color: #ff9900;">FontSize</span>'<span style="color: #000000;">,20</span></span>)</span>
<span style="color: #3366ff;">xlabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">time (s)</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">ylabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">speed (m/s)</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">title(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Rocket Projectile</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">grid(<span style="color: #ffcc00;"><span style="color: #800080;">'</span><span style="color: #ff9900;">minor</span><span style="color: #800080;">'</span></span>)</span>
 
<span style="color: #3366ff;">legend(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Rocket1</span><span style="color: #800080;">'</span><span style="color: #000000;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">Location</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">SouthEast</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

This gives us the following:

Now supposing we want to create a plot of v vs t and also a second Rocket that is twice as fast 2*v vs t:

<span style="color: #0000ff;">figure(</span>1<span style="color: #0000ff;">)</span>
<span style="color: #3366ff;">plot(</span>t,v,...
<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,<span style="color: #800080;"><span style="color: #000000;"><span style="color: #808080;"><span style="color: #000000;">[</span><span style="color: #ff0000;">255/255</span><span style="color: #000000;">,</span><span style="color: #00ff00;">192/255</span><span style="color: #000000;">,</span><span style="color: #0000ff;">0/255</span><span style="color: #000000;">]</span></span></span></span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'<span style="color: #ff9900;">:</span></span><span style="color: #800080;">'</span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5,<span style="color: #ff00ff;">...</span>
<span style="color: #993366;">'</span><span style="color: #ff9900;">Marker</span><span style="color: #993366;">'</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">s</span><span style="color: #993366;">',<span style="color: #ff00ff;">...</span>
'<span style="color: #ff9900;">MarkerSize</span>'<span style="color: #000000;">,20,</span><span style="color: #ff00ff;">...</span>
</span></span></span><span style="color: #3366ff;"><span style="color: #800080;"><span style="color: #000000;"><span style="color: #993366;">'<span style="color: #ff9900;">MarkerEdge</span>'<span style="color: #000000;">,[</span><span style="color: #ff0000;">0/255</span>,<span style="color: #00ff00;">176/255</span>,<span style="color: #0000ff;">80/255</span><span style="color: #000000;">],</span><span style="color: #ff00ff;">...
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFace</span><span style="color: #800080;">'</span> <span style="color: #000000;">,[</span><span style="color: #ff0000;">112/255</span><span style="color: #000000;">,</span><span style="color: #00ff00;">48/255<span style="color: #000000;">,</span></span><span style="color: #0000ff;">160/255</span><span style="color: #000000;">]</span></span></span></span></span>)</span>
<span style="color: #3366ff;">plot(</span>t,2*v,...
<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,<span style="color: #800080;"><span style="color: #000000;"><span style="color: #808080;"><span style="color: #ff0000;"><span style="color: #000000;">[</span>192/255</span><span style="color: #000000;">,</span><span style="color: #00ff00;">0/255</span><span style="color: #000000;">,</span><span style="color: #0000ff;">0/255</span><span style="color: #000000;">]</span></span></span></span><span style="color: #000000;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'<span style="color: #ff9900;">:</span></span><span style="color: #800080;">'</span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5,<span style="color: #ff00ff;">...</span>
<span style="color: #993366;">'</span><span style="color: #ff9900;">Marker</span><span style="color: #993366;">'</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">s</span><span style="color: #993366;">'<span style="color: #000000;">,</span><span style="color: #ff00ff;">...</span>
'<span style="color: #ff9900;">MarkerSize</span>'<span style="color: #000000;">,20,</span><span style="color: #ff00ff;">...</span>
</span></span></span><span style="color: #3366ff;"><span style="color: #800080;"><span style="color: #000000;"><span style="color: #993366;">'<span style="color: #ff9900;">MarkerEdge</span>'<span style="color: #000000;">,[</span><span style="color: #ff0000;">0/255</span>,<span style="color: #00ff00;">176/255</span><span style="color: #000000;">,</span><span style="color: #0000ff;">80/255</span><span style="color: #000000;">],</span><span style="color: #ff00ff;">...
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFace</span><span style="color: #800080;">'</span> <span style="color: #000000;">,[</span><span style="color: #ff0000;">112/255</span><span style="color: #000000;">,</span><span style="color: #00ff00;">48/255</span><span style="color: #000000;">,</span><span style="color: #0000ff;">160/255</span><span style="color: #000000;">]</span></span></span></span></span>)</span>
<span style="color: #0000ff;">set(</span>gca,<span style="color: #ff00ff;">...<span style="color: #000000;">,</span></span><span style="color: #0000ff;"><span style="color: #800080;">
'<span style="color: #ff9900;">FontSize</span>'<span style="color: #000000;">,20</span></span>)</span>
<span style="color: #3366ff;">xlabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">time (s)</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">ylabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">speed (m/s)</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">title(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Rocket Projectile</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">grid(<span style="color: #ffcc00;"><span style="color: #800080;">'</span><span style="color: #ff9900;">minor</span><span style="color: #800080;">'</span></span>)</span>
 
<span style="color: #3366ff;">legend(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Rocket1</span><span style="color: #800080;">'</span><span style="color: #000000;">,<span style="color: #800080;">'</span><span style="color: #ff9900;">Rocket2</span><span style="color: #800080;">'</span></span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">Location</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">SouthEast</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

This code overwrites the data on Figure 1 opposed to adding a second Line Plot.

To add additional lines to the Figure 1 opposed to writing over them we need to use the

<span style="color: #0000ff;">hold</span>

function with the input argument

<span style="color: #3366ff;"><span style="color: #800080;">'<span style="color: #ff9900;">on</span>'</span></span>

. We should use the 

<span style="color: #0000ff;">hold</span>

function with the input argument

<span style="color: #3366ff;"><span style="color: #800080;">'<span style="color: #ff9900;">off</span>'</span></span>

 to release after we've finished plotting.

<span style="color: #0000ff;">figure(</span>1<span style="color: #0000ff;">)</span>
<span style="color: #3366ff;">plot(</span>t,v,...
<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,<span style="color: #800080;"><span style="color: #000000;"><span style="color: #808080;"><span style="color: #000000;">[</span><span style="color: #ff0000;">255/255</span><span style="color: #000000;">,</span><span style="color: #00ff00;">192/255</span><span style="color: #000000;">,</span><span style="color: #0000ff;">0/255</span><span style="color: #000000;">]</span></span></span></span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'<span style="color: #ff9900;">:</span></span><span style="color: #800080;">'</span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5,<span style="color: #ff00ff;">...</span>
<span style="color: #993366;">'</span><span style="color: #ff9900;">Marker</span><span style="color: #993366;">'</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">s</span><span style="color: #993366;">'<span style="color: #000000;">,</span><span style="color: #ff00ff;">...</span>
'<span style="color: #ff9900;">MarkerSize</span>'<span style="color: #000000;">,20,</span><span style="color: #ff00ff;">...</span>
</span></span></span><span style="color: #3366ff;"><span style="color: #800080;"><span style="color: #000000;"><span style="color: #993366;">'<span style="color: #ff9900;">MarkerEdge</span>'<span style="color: #000000;">,[</span><span style="color: #ff0000;">0/255</span><span style="color: #000000;">,</span><span style="color: #00ff00;">176/255</span><span style="color: #000000;">,</span><span style="color: #0000ff;">80/255</span><span style="color: #000000;">],</span><span style="color: #ff00ff;">...
'<span style="color: #ff9900;">MarkerFace</span>' <span style="color: #000000;">,[</span><span style="color: #ff0000;">112/255</span>,<span style="color: #00ff00;">48/255,</span><span style="color: #0000ff;">160/255</span><span style="color: #000000;">]</span></span></span></span></span>)</span>
<span style="color: #3366ff;">hold(<span style="color: #800080;">'<span style="color: #ff9900;">on</span>'</span>)
plot(</span>t,2*v,...
<span style="color: #800080;">'</span><span style="color: #ff9900;">Color</span><span style="color: #800080;">'</span>,<span style="color: #800080;"><span style="color: #000000;"><span style="color: #808080;"><span style="color: #ff0000;">[192/255</span>,<span style="color: #00ff00;">0/255</span>,<span style="color: #0000ff;">0/255</span>]</span></span></span><span style="color: #000000;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'<span style="color: #ff9900;">:</span></span><span style="color: #800080;">'</span>,<span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'<span style="color: #000000;">,5,<span style="color: #ff00ff;">...</span>
<span style="color: #993366;">'</span><span style="color: #ff9900;">Marker</span><span style="color: #993366;">'</span>,<span style="color: #993366;">'</span><span style="color: #ff9900;">s</span><span style="color: #993366;">'<span style="color: #000000;">,</span><span style="color: #ff00ff;">...</span>
'<span style="color: #ff9900;">MarkerSize</span>'<span style="color: #000000;">,20,</span><span style="color: #ff00ff;">...</span>
</span></span></span><span style="color: #3366ff;"><span style="color: #800080;"><span style="color: #000000;"><span style="color: #993366;">'<span style="color: #ff9900;">MarkerEdge</span>'<span style="color: #000000;">,[</span><span style="color: #ff0000;">0/255</span><span style="color: #000000;">,</span><span style="color: #00ff00;">176/255</span><span style="color: #000000;">,</span><span style="color: #0000ff;">80/255</span><span style="color: #000000;">],</span><span style="color: #ff00ff;">...
'<span style="color: #ff9900;">MarkerFace</span>' <span style="color: #000000;">,[</span><span style="color: #ff0000;">112/255</span><span style="color: #000000;">,</span><span style="color: #00ff00;">48/255</span><span style="color: #000000;">,</span><span style="color: #0000ff;">160/255</span><span style="color: #000000;">]</span></span></span></span></span>)
hold(<span style="color: #800080;">'<span style="color: #ff9900;">off</span>'</span>)</span>
<span style="color: #0000ff;">set(</span>gca,<span style="color: #ff00ff;">...<span style="color: #000000;">,</span></span><span style="color: #0000ff;"><span style="color: #800080;">
'<span style="color: #ff9900;">FontSize</span>'<span style="color: #000000;">,20</span></span>)</span>
<span style="color: #3366ff;">xlabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">time (s)</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">ylabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">speed (m/s)</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">title(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Rocket Projectile</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">grid(<span style="color: #ffcc00;"><span style="color: #800080;">'</span><span style="color: #ff9900;">minor</span><span style="color: #800080;">'</span></span>)</span>
 
<span style="color: #3366ff;">legend(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Rocket1</span><span style="color: #800080;">'</span><span style="color: #000000;">,<span style="color: #800080;">'</span><span style="color: #ff9900;">Rocket2</span><span style="color: #800080;">'</span></span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">Location</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">NorthWest</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

Because these are commonly used, there is an abbreviated form:

<span style="color: #0000ff;">hold </span><span style="color: #3366ff;"><span style="color: #800080;"><span style="color: #ff9900;">on</span></span></span>
<span style="color: #0000ff;">hold</span><span style="color: #0000ff;"><span style="color: #800080;"> </span></span><span style="color: #3366ff;"><span style="color: #800080;"><span style="color: #ff9900;">off</span></span></span>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.