CSE1121 Chapter4
CSE1121 Chapter4
Scientific Programming
ENGR 101
Introduction to Programming
Plotting
Two Dimensional Plots
• The x-y plot is the most commonly used plot by
engineers
• The independent variable is usually called x
• The dependent variable is usually called y
Consider this x-y data
time, sec Distance, Ft
0 0 Time is the
2 0.33 independent
variable and
4 4.13
distance is the
6 6.29 dependent
8 6.85 variable
10 11.19
12 13.19
14 13.96
16 16.33
18 18.17
Define x and y and call the plot
function
The second
line is also
drawn in blue,
on top of the
original plot
To unfreeze the plot
use the hold off
command
You can also create multiple lines on a
single graph with one command
Matrix of Y
values
Line, Color and Mark Style
• You can change the appearance of your plots by
selecting user defined
• line styles
• color
• mark styles
• Try using
help plot
for a list of available styles
Line Specifier: Available
choices
Specify your choices in a string
• For example
• plot(x,y,':ok')
• strings are identified with a tick mark
• if you don’t specify style, a default is used
• line style – none
• mark style – none
• color - blue
plot(x,y,':ok')
• In this command
• the : means use a dotted line
• the o means use a circle to mark each point
• the letter k indicates that the graph should be drawn in
black
• (b indicates blue)
dotted line
circles
black
specify the
drawing
parameters for
each line after
the ordered pairs
that define the
line
For more details
>>help plot
Axis scaling
• MATLAB automatically scales each plot to
completely fill the graph
• If you want to specify a different axis – use the axis
command
axis([xmin,xmax,ymin,ymax])
• Lets change the axes on the graph we just looked at
Use the axis
function to
override the
automatic
scaling
Additional Annotations
• You can also add
• legends
• textbox
• Of course you should always add
• title
• axis labels
Improving your labels
You can use Greek letters in your labels by putting a
backslash (\) before the name of the letter. For
example:
title(‘\alpha \beta \gamma’)
creates the plot title
αβγ
Superscripts and Subscripts
To create a superscript use caret
title(‘x^2’)
gives
x2
title(‘x_2’)
gives
x2
Use curly brackets if you have more than one element in your
sub/superscript
title(‘x^{-255}’)
gives
x-255
Tex Markup Language
• These label improvements use the Tex Markup
Language
• Use the Help feature to find out more!
>>help text properties
Subplots
• The subplot command allows you to subdivide the
graphing window into a grid of m rows and n
columns
• subplot(m,n,p)
rows columns location
subplot(2,2,1)
2 columns
Peaks
-5
1 2
2
0 2
0
2 rows y
-2 -2
x
3 4
2 rows and 1
column
Other Types of 2-D Plots
• Polar Plots
• Logarithmic Plots
• Bar Graphs
• Pie Charts
• Histograms
• X-Y graphs with 2 y axes
• Function Plots
Polar Plots
• Some functions are easier to specify using polar
coordinates than by using rectangular coordinates
• For example the equation of a circle is
• y=sin(x)
in polar coordinates
Logarithmic Plots
• A logarithmic scale (base 10) is convenient when
• a variable ranges over many orders of magnitude,
because the wide range of values can be graphed,
without compressing the smaller values.
• data varies exponentially.
x-y plot – linear on
both axes
E > 0 to 60
D > 60 to 70
C > 70 to 80
B > 80 to 90
A > 90 to 100
>> bin_edge=[0,60,70,80,90,100.1];
>> bar(bin_edge,n)
X-Y Graphs with Two Y Axes
• Sometimes it is useful to overlay two x-y plots onto
the same figure. However, if the order of
magnitude of the y values are quite different, it may
be difficult to see how the data behave.
For example
Scaling Depends on the largest
value plotted
• Its difficult to see how the blue line behaves, because the
scale isn’t appropriate
The plotyy
function allows
you to use two
scales on a
single graph
Different Types of Plots with two Y-axes
Adding Labels
function input as a
string range of the independent
variable – in this case x
Three Dimensional Plotting
• Line plots
• Surface plots
• Contour plots
Three Dimensional Line Plots
• These plots require a set of order triples ( x-y-z
values) as input
Explore the
property editor to
see some of the
other ways you Change the
can adjust your Aspect Ratio
plot interactively Select Inspector
from the
Property Editor
Saving your plots
• Rerun your M-file to recreate a plot
• Save the figure from the file menu using the save
as… option
• You’ll be presented with several choices of file format
such as
• jpeg
• emg (enhanced metafile) etc
• Right-click on the figure and select copy – then
paste it into another document
• >> saveas(gcf,'myFigure.png')
• >>saveas(figure(2),'mfilename3.png')