0% found this document useful (0 votes)
13 views

Lecture Note #5

This document provides information about plotting graphs in MATLAB for civil engineering applications. It discusses how to create 2D and 3D plots using the plot command in MATLAB. Various plot options are described, including line styles, marker types, colors, labels, legends, axis limits, grids, and adding text. Examples are provided to demonstrate plotting simple functions like sine, cosine and their combinations in multiple subplots. The document aims to teach the basics of visualizing data using MATLAB's plotting tools which are useful for civil engineering applications.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture Note #5

This document provides information about plotting graphs in MATLAB for civil engineering applications. It discusses how to create 2D and 3D plots using the plot command in MATLAB. Various plot options are described, including line styles, marker types, colors, labels, legends, axis limits, grids, and adding text. Examples are provided to demonstrate plotting simple functions like sine, cosine and their combinations in multiple subplots. The document aims to teach the basics of visualizing data using MATLAB's plotting tools which are useful for civil engineering applications.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

CENG103:

Computer Programming &


Applications for Civil Engineering
Lecture Note #5: Plotting Graphs in MATLAB

Prepared by: Sayed Yousif Arif Mohammed


About Plotting in MATLAB
 Plotting tools in MATLAB help the user visualize the data represented in a
matrix with ease.

 MATLAB contains a huge category of plots that can be used to represent the
data provided, however in this course we will focus on two types of plots
only:
1. 2-D graphs using linear axis: to plot 2-D data

2. 3-D graph using linear axis: to plot 3-D data.

 The concept that will be studied will remain the same for all the other types
of plots.

2
MATLAB – 2D Plot
 To plot a 2D graph, the command used is a function called “plot”.
 There are various of ways to write the “plot” function is MATLAB:
1. To create a 2D line plot of the data in a variable named a versus the index of each value.
>> plot(a)
2. To create a 2D line plot of the data in a variable named a versus the corresponding values
in a variable named b. The size of a and b must be equal.
>> plot(a,b)
3. To create multiple 2D line plots of the data in a list of variables named 𝑎𝑎1 , 𝑎𝑎2 , … 𝑎𝑎𝑛𝑛 versus
the corresponding values in a list of variables named 𝑏𝑏1 , 𝑏𝑏2 , … 𝑏𝑏𝑛𝑛 , respectively. The size
of each two variables plotted against each other must be equal.
>> plot(𝑎𝑎1 ,𝑏𝑏1 ,𝑎𝑎2 ,𝑏𝑏2 ,...𝑎𝑎𝑛𝑛 ,𝑏𝑏𝑛𝑛 )
4. To create a 2D line plot of the data in a variable named a versus the corresponding values
in a variable named b with a specific line specifications.
>> plot(𝑎𝑎,𝑏𝑏,𝐿𝐿𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖)
5. To create multiple 2D line plots of the data in a list of variables named 𝑎𝑎1 , 𝑎𝑎2 , … 𝑎𝑎𝑛𝑛
versus the corresponding values in a list of variables named 𝑏𝑏1 , 𝑏𝑏2 , … 𝑏𝑏𝑛𝑛 , respectively.
With a list of specific line specifications.
>> plot(𝑎𝑎1 ,𝑏𝑏1 ,𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿,𝑎𝑎2 ,𝑏𝑏2 ,𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿2,...𝑎𝑎𝑛𝑛 ,𝑏𝑏𝑛𝑛 ,𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝑛𝑛)

3
MATLAB – 2D Plot
 Linespec argument can be used to specify the line styles, marker types, and colors
which will be displayed using string specifiers, detailed in the following table:
Line style Marker type Color
Specifier Indication Specifier Indication Specifier Indication
'-' Solid line (default) '+' Plus sign r Red

'--' Dashed line 'o' Circle g Green

':' Dotted line '*' Asterisk b Blue

'-.' Dash-dot line '.' Point c Cyan


(none) No line 'x' Cross m Magenta

'square' or 's' Square y Yellow

'diamond' or 'd' Diamond k Black

'^' Upward-pointing triangle w White

'v' Downward-pointing triangle

'>' Right-pointing triangle

'<' Left-pointing triangle

'pentagram' or 'p' Five-pointed star (pentagram)

'hexagram' or 'h''' Six-pointed star (hexagram)


4
MATLAB – 2D Plot
Examples: The following graphs are output after executing the following commands
>> a=[-10:10];
>> b=a.^3;
>> c=a.^2*8;
>> plot (b) >> plot (a,b)

5
MATLAB – 2D Plot
Examples: The following graphs are output after executing the following commands
>> a=[-10:10];
>> b=a.^3;
>> c=a.^2*8;
>> plot (a,b,a,c) >> plot (a,b,a,c, [-5:1:5],[-5:1:5].^2*50)

6
MATLAB – 2D Plot
Examples: The following graphs are output after executing the following commands
>> a=[-10:10];
>> b=a.^3;
>> c=a.^2*8;
>> plot (a,b,'-.ro') >> plot (a,b,'--ms',a,c,'b:',a,b*.5,'^g')

7
MATLAB – 2D Plot
 Another additional option is to also control the line width as well as the marker’s
size and its edge and face (i.e., interior) colors. To do so, an additional argument
has to be introduced which is a string containing the property which is required
to be modified.
 For example, the following command uses a heavier (2-point), dashed, cyan line
to connect larger (10-point) diamond-shaped markers with black edges and
magenta faces
>>plot(a,b,'--dc','LineWidth’,2,'MarkerSize',10,'MarkerEdgeColor','k','MarkerFaceColor','m’)

Note
The default line width is 1 point. For
the markers, the default size is 6
point with blue edge color and no
face color.

8
MATLAB – 2D Plot
 When the plot function is executed multiple times, at each time the previous
plots are erased every time.
 An available command in MATLAB is the hold on command which holds the
current plot and all axis properties so that additional graphing commands can be
added to the existing plot.
 After completing the plot, hold off command returns to the default mode.
 For example, if we had typed the following commands, the final plot would only
display symbols

>> plot (a,b)


>> plot (a,b) >> hold on
>> plot (a,c) >> plot (a,c)
>> hold off

9
MATLAB – 2D Plot
 An additional options that can introduce more details to the plotted chart can be
done through the following functions:
1. To label the x-axis of the current axes:
>> xlabel('string')
2. To label the y-axis of the current axes:
>> ylabel('string')
3. To add the chart title. It is placed at the top and in the center of the current
axes:
>> title('string')
4. To add the written string to the location specified by the point (x,y). x and y
must be numbers:
>> text(x,y,'string')
5. To place a legend on graph. The number of strings have to be the same as
number of plotted lines:
>> legend('string1', 'string2',… 'stringn')

10
MATLAB – 2D Plot
6. To add major grid lines to the current axes:
>> grid on
7. To remove major grid lines to the current axes:
>> grid off
8. To set the axis bounds in the x axes to the specified values:
>> xlim([xmin xmax])
9. To set the axis bounds in the y axes to the specified values:
>> ylim([ymin ymax])
10. To divides the current figure into an m-by-n grid and creates an axes in the grid
position specified by p:
>> subplot(m,n,p)

11
MATLAB – 2D Plot
Example 1: For the same a, b, and c values described in the example in slides 5
through 7, plot a vs. b and a vs. c and display a label mentioning ‘x values’ on x axis
and ‘y values’ on y axis. Additionally, display the equations plotted in the legend
and plot a title stating ‘x vs. y plot example’. Also, display a text stating ‘text at
position’ at (-8.5,700). Finally, turn the gridlines on.
>> a=[-10:10];
>> b=a.^3;
>> c=a.^2*8;
>> plot(a,b,a,c)
>> xlabel('x values')
>> ylabel('y values')
>> legend('y = x^3','y= x^2*8')
>> title('x vs. y plot example')
>> text(-8.5,700,'text at position')
>> grid on
12
MATLAB – 2D Plot
Example 2: Plot 2 cycles of sin function starting (from 0 to 4𝜋𝜋) using 100 linearly
spaced values. Make the plot red colored, with dotted line, and circle markers.
Display a label mentioning ‘Angle in radians’ on x axis and ‘sin value’ on y axis.
Additionally, limit the display bounds of x-axis to 0 ~ 4𝜋𝜋. Finally, turn the gridlines
on.
>> x=linspace(0,4*pi,100);
>> y=sin(x);
>> plot(x,y,':ro');
>> xlabel('Angle in radians');
>> ylabel('sin value’);
>> title('Sine curve’);
>> xlim([0 4*pi]);
>> grid on;

13
MATLAB – 2D Plot
Example 3: Plot 2 cycles of sin, cos, and tan functions in three separate subplots
each starting (from 0 to 4𝜋𝜋) using 100 linearly spaced values. Make the plot of the
sine curve red colored, cosine curve green colored, and tan curve black colored.
Display a label on each subplot mentioning ‘Angle in radians’ on x axis and
‘sin/cos/tan value’ on y axis, respectively. Additionally, for all the subplots limit the
display bounds of x-axis to 0 ~ 4𝜋𝜋 and y-axis to -1 ~ 1 (for tan plot only, the y-axis
values should be from -5 to 5). Finally, turn the gridlines on.

Variables definition Plotting Sine curve Plotting Cosine curve Plotting Tangent curve
>> x=linspace(0,4*pi,100); >> subplot(2,2,1) >> subplot(2,2,2) >> subplot(2,2,[3 4])
>> y1=sin(x); >> plot(x,y1,'r'); >> plot(x,y2,'g’); plot(x,y3,'k');
>> y2=cos(x); >> xlabel('Angle in radians'); >> xlabel('Angle in radians’); xlabel('Angle in radians');
>> y3=tan(x); >> ylabel('sin value'); >> ylabel('cos value’); ylabel('tan value');
>> xlim([0 4*pi]); >> xlim([0 4*pi]); xlim([0 4*pi]);
>> ylim([-1 1]); >> ylim([-1 1]); ylim([-5 5]);
>> grid on; >>grid on; grid on;

14
MATLAB – 2D Plot

15
MATLAB – 3D Plot
 To plot a 3D graph, the command used is a function called “plot3”.
 There are various of ways to write the “plot” function is MATLAB:
1. To create a 3D line plot of the data in a variable named a versus the
corresponding values in variables named b and c. The size of a, b and c must be
equal.
>> plot3(a,b,c)
2. To create multiple 3D line plots of the data in a list of variables named 𝑎𝑎1 , 𝑎𝑎2 ,
… 𝑎𝑎𝑛𝑛 versus the corresponding values in a list of variables named 𝑏𝑏1 , 𝑏𝑏2 , … 𝑏𝑏𝑛𝑛
and 𝑐𝑐1 , 𝑐𝑐2 , … 𝑐𝑐𝑛𝑛 , respectively. The size of each three variables plotted against
each other must be equal.
>> plot3(𝑎𝑎1 ,𝑏𝑏1 ,𝑐𝑐1 ,𝑎𝑎2 ,𝑏𝑏2 ,𝑐𝑐2 ,...𝑎𝑎𝑛𝑛 ,𝑏𝑏𝑛𝑛 ,𝑐𝑐𝑛𝑛 )
3. To create a 3D line plot of the data in a variable named a versus the
corresponding values in variables named b and c with a specific line
specifications.
>> plot3(𝑎𝑎,𝑏𝑏,𝑐𝑐,𝐿𝐿𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖)
4. To create multiple 3D line plots of the data in a list of variables named 𝑎𝑎1 , 𝑎𝑎2 , …
𝑎𝑎𝑛𝑛 versus the corresponding values in a list of variables named 𝑏𝑏1 , 𝑏𝑏2 , … 𝑏𝑏𝑛𝑛 and
𝑐𝑐1 , 𝑐𝑐2 , … 𝑐𝑐𝑛𝑛 , respectively. With a list of specific line specifications.
>> plot3(𝑎𝑎1 ,𝑏𝑏1 ,𝑐𝑐1 ,𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿,𝑎𝑎2 ,𝑏𝑏2 ,𝑐𝑐2 ,𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿2,...𝑎𝑎𝑛𝑛 ,𝑏𝑏𝑛𝑛 ,𝑐𝑐𝑛𝑛 ,𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝐿𝑛𝑛)
16
MATLAB – 3D Plot
 The same line specifications applicable in 2D plots are also applicable in 3D
plots.
 In addition to the functions used in 2D plots, 3D plots can control the z axis label
and bounds using the following two equations respectively.
1. To label the z-axis of the current axes:
>> zlabel('string’)
2. To set the axis bounds in the z axes to the specified values:
>> zlim([zmin zmax])

17
MATLAB – 3D Plot
Example: Plot x = sin(t), y = cos(t), and z = t for t = 0 to 10𝜋𝜋 with 500 linearly
spaced points. Display the plotted line in red colour and label the three axis with
‘sin(t)’, ‘cos(t)’, and ‘t’ for x, y, and z axes respectively. Additionally, limit the
bounds of z-axis to 0 ~ 30. Finally, turn on the gridline.
>> t = linspace(0,10*pi,500);
>> x=sin(t);
>> y=cos(t);
>> z=t;
>> plot3(x,y,z,'r')
>> xlabel('sin(t)')
>> ylabel('cos(t)')
>> zlabel('t')
>> zlim([0 30])
>> grid on

18
MATLAB – Saving the Plot
 It is possible to save the plotted figure simply by selecting the save tool in the
figure window as shown in picture 1.
 The plotted figure can be saved in many formats as shown in picture 2.

Picture 2

Picture 1 19

You might also like