2 Plots Matlab

L. Oberbroeckling, Spring 2018.

Contents

This document gives BASIC ways to color graphs in MATLAB. See

Types of MATLAB Plots. There are various functions that you can use to plot data in MATLAB ®.This table classifies and illustrates the common graphics functions. A line drawn with Matlab is feasible by incorporating a 2-D plot function plot that creates two dimensional graph for the dependent variable with respect to the depending variable. Matlab supports plotting multiple lines on single 2D plane. The lines drawn from plot function can be continuous or discrete by nature.

and

for more in-depth explanations and fancier coloring, to name just two sources.

Default Colors in 2D Graphs

The default colors used in MATLAB changed in R2014b version. Here are the colors, in order, and their MATLAB RGB triplet.

Current colorOld color
[0, 0.4470, 0.7410][0, 0, 1]
[0.8500, 0.3250, 0.0980][0, 0.5, 0]
[0.9290, 0.6940, 0.1250][1, 0, 0]
[0.4940, 0.1840, 0.5560][0, 0.75, 0.75]
[0.4660, 0.6740, 0.1880][0.75, 0, 0.75]
[0.3010, 0.7450, 0.9330][0.75, 0.75, 0]
[0.6350, 0.0780, 0.1840][0.25, 0.25, 0.25]

Another thing that changed starting in the R2014b version is that the hold on and hold off automatically cycles through the colors. In the past, each new plot command would start with the first color (blue) and you would have to manually change the color. Now it will automatically move to the next color(s). See below for how to manually adjust the colors.

Default Colors in 3D Graphs

If using mesh(x,y,z), to change the look of it you would want to change 'EdgeColor'. Note that the name of this colormap is 'parula' while previous to R2014b, it was 'jet'

Using Basic Colors in Graphs

The eight basic colors are known by either their short name or long name (RGB triplets are also included).

Long NameShort NameRGB Triplet
blueb[0,0,1]
blackk[0,0,0]
redr[1,0,0]
greeng[0,1,0]
yellowy[1,1,0]
cyanc[0,1,1]
magentam[1,0,1]
whitew[1,1,1]

Example of how to change the color using short names is below. You can easily do the same thing using the long names.

Changing Colors

Many times you want to have more control of what colors are used. For example, I may want some data points drawn in the same color as the curve. Or I have a piece-wise graph that I want to have all the same color. There are several ways to do this. One is to use the default colors and 'resetting' the order, which is shown here. Others involve using the RGB triplet (see next section).

As you may see, this could get confusing to keep track of. Thus it may be easier to use the RGB triplets, and even name them ahead of time. This is discussed in the section below.

Using RGB triplets to change colors

One can specify colors using a vector that gives the RGB triple where in MATLAB, each of the three values are numbers from 0 to 1. Usually RGB colors have values from 0 to 255. You can use those numbers and divide the vector by 255 to use within MATLAB. Thus knowing the MATLAB RGB triples for the colors can be useful. From the table above, we can define the default colors to work with them or can put in the RGB triplet (as a vector) directly into the plot command. Both are shown in this example.

For other colors, you can look up their RGB code on many websites such as RGB Color Codes Chart or HTML Color Picker to see the RGB codes (or hex codes, etc.) For example, at these RGB Color websites, you will be given R=255, G=0, B=0 for red. So you can use 1/255[255,0,0] to get the color of red to use as a color in MATLAB.

The official color for Loyola Green is given as RGB:0-104-87, and Loyola Gray is given as RGB:200-200-200 (found on Loyola's Logos/University Signature page. Here's how one can use those colors in MATLAB.

Now one can use these colors to specify the color of markers, lines, edges, faces, etc.

Changing colors in 3D Graphs

If using mesh(x,y,z), to change the look of it you can change to a different colormap as discussed in https://www.mathworks.com/help/matlab/ref/colormap.html. This was done above when showing the previous default colormap. Here are some more.

Warning! Once you change the colormap, it will keep that colormap for all subsequent 3D plots within the same figure or MATLAB session until you use close, or open a new figure window.

For mesh and surf, you can change 'EdgeColor' and/or 'FaceColor' to be uniform, rather than using colormaps.


Published with MATLAB® R2016a

Multiple plots

The plot command can plot several sets of vectors.

Plot the functions y1 = sin(2 pi x) and y2 = cos(2 pi x) for x in the interval [0, 1] using 401 equally spaced points.

Create a vector x of 401 equally spaced points on [0, 1].

Create a vector y1 of function values.

Create a vector y2 of function values.

Plot both sets points in the same figure.

Add a grid.

Add a title. What is still missing?

>> y1 = sin(2*pi*x);
>> plot(x, y1, x, y2)
>> title('sin(2 pi x) and cos(2 pi x)')

In general you can use

plot(x1, y1, s1, x2, y2, s2, x3, y3, s3)

where x1 and y1 are vectors of the same length and s1 is an optional string.

Legends

When there are multiple plots in the same figure it is a good idea to add a legend, using, for example,

legend(string1, string2, string3)

Here string1 is a string describing the first set of values plotted, string2 is a string describing the second set of values plotted, and string3 is a string describing the third set of values plotted.

You can use the mouse to reposition the legend box within the plot, or you can specify the location of the legend box. See

help legend

for more information.

The first string describes the first data set, the second string the second data set.
2 Plots Matlab

The hold command

If you have already created a plot and later wish to add another plot, then the hold command is useful.

Create the previous plot using two separate plot commands and the hold command.

Create the first plot assuming x, y1 and y2 are defined as above.

Turn the hold on.

Add the second plot.

Turn the hold off.

>> hold on
>> hold off

Note that when using a single plot command, MATLAB adjusts the colours for successive plots. When using the hold command you must explicitly set the colours, for example using plot(x, y2, 'g').

Subplots

Sometimes you want a single figure containing several individual subplots. The MATLAB command

subplot(m, n, k)

creates an m by n array of plots and positions you at plot number k, where the plots are numbered counting across rows.

The most common examples are

  • a 2 by 1 grid of subplots for two plots one on top of each other;
  • a 1 by 2 grid for two plots side by side.

Here is a 2 by 2 grid of subplots to make it clear how the numbering of subplots works.

Create plots of sin(k pi x) for x on [0, 1] and k = 1, 2, 3, 4 in one figure.

Create vector of plot points.

2 by 2 grid of subplots, at subplot 1.

Add plot on current subplot.

Add grid and title on current subplot.

2 by 2 grid of subplots, at subplot 2.

Add plot on current subplot.

Add grid and title on current subplot.

2 by 2 grid of subplots, at subplot 3.

Add plot on current subplot.

Add grid and title on current subplot.

2 by 2 grid of subplots, at subplot 4.

Add plot on current subplot.

Add grid and title on current subplot.

>> subplot(2,2,1)
>> grid on; title('k = 1')

Multiple Plots Matlab Legend

>> plot(x, sin(2*pi*x))
>> subplot(2,2,3)
>> grid on; title('k = 3')
>> plot(x, sin(4*pi*x))

Axis limits

If you are really observant you will have noticed that the limits of the y-axis on the first subplot is from 0 to 1, while the other three plots all have y ranging from -1 to 1. MATLAB tries to choose good axis limits based on the data that is being plotted. However sometimes you want to change the axis limits.

The simplest way to do this is to use

xlim([xmin xmax])

to make the x-axis run from xmin to xmax. To adjust the limits of the y-axis use

ylim([ymin ymax])

to make the y-axis run from ymin to ymax. Note that the argument to xlim and ylim is a two element vector giving the lower and upper limits for the axis.

Another way to control the limits and scaling of the axes is to use the axis command, for example

axis([xmin xmax ymin ymax])

By default the x-scale is slightly larger than the y-scale, so you get a rectangular plot on the screen. To make the axis scaling equal use

axis equal

which is required to make a circle look like a circle!

As always, more information can be obtained by typing

help axis

in the MATLAB command window.

Plot the two circles (cos(t), sin(t)) and (cos(t), sin(t))/2 for t in [0, 2 pi]. Ensure the plot looks like circles not ellipses! Make both the x-axis and y-axis both go form -1.1 to 1.1.

Create vector t of parameter values.

Store the values of cos(t) and sin(t).

Plot the two curves.

Make the axis scaling equal.

Adjust the axis limits.

Add a grid.

>> ct = cos(t); st = sin(t);
>> axis equal
>> grid on

Figures

MATLAB draws a plot in the current figure window. Figure windows are labelled 1, 2, 3, ... The command

figure

creates a new figure window. Alternatively the command

figure(k)

where k is a positive integer, opens figure window k if it already exists, or creates figure window k if it does not exist.

Self-test Exercise

2 Plots Matlab Worksheet

Plot the functions sin(x), x, and 2x/pi over the interval [0, pi/2], including the title 'Bounds on sin(x)', a grid, a legend and making sure the x-axis corresponds to the plot interval.

Answer:
x = linspace(0, pi/2, 401);
plot(x, sin(x), x, x, x, 2*x/pi);
title('Bounds on sin(x)')
grid on
legend('sin(x)', 'x', '2x/pi')
xlim([0, pi/2])
Use the mouse to select the text between the word 'Answer' and here to see the answer.

Summary

The plot command can plot several sets of data on the one set of axes. In this case a legend should be added.

Several different plots within the one figure can be created using the subplot command.

Matlab Plot Two Series

Axis limits and scaling can be modified with the xlim, ylim and axis commands.