Matlab Training Sessions 6,7: Plotting - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Matlab Training Sessions 6,7: Plotting

Description:

Matlab has a powerful plotting engine that can generate a wide ... Colorbar( vert') adds a vertical colorbar. Colorbar( horiz) adds a horizontal colorbar ... – PowerPoint PPT presentation

Number of Views:1032
Avg rating:3.0/5.0
Slides: 41
Provided by: DML92
Category:

less

Transcript and Presenter's Notes

Title: Matlab Training Sessions 6,7: Plotting


1
Matlab Training Sessions 6,7Plotting
2
  • Course Outline
  • Weeks
  • Introduction to Matlab and its Interface (Jan 13
    2009)
  • Fundamentals (Operators)
  • Fundamentals (Flow)
  • Importing Data
  • Functions and M-Files
  • Plotting (2D and 3D)
  • Statistical Tools in Matlab
  • Analysis and Data Structures
  • Course Website
  • http//www.queensu.ca/neurosci/matlab.php

3
  • Week 5 Lecture Outline
  • Plotting Data
  • Basics
  • Generating data
  • 2D Plots (Line, Scatter, Bar)
  • D. Plot Features

4
Basics
  • Matlab has a powerful plotting engine that can
    generate a wide variety of plots.

5
Generating Data
  • Matlab does not understand functions, it can only
    use arrays of numbers.
  • at2
  • bsin(2pit)
  • ce-10t note matlab command is exp()
  • dcos(4pit)
  • e2t3-4t2t
  • Generate it numerically over specific range
  • Try and generate a-e over the interval 00.012

t00.0110 make x vector yt.2 now we have
the appropriate y but only over the
specified range
6
Line/Scatter
  • Simplest plot function is plot()
  • Try typing plot(y)
  • Matlab automatically generates a figure and draws
    the data and connects its lines
  • Looks right, but the x axis units are incorrect
  • Type plot(x,y), will look identical but have
    correct x-axis units
  • Plot(x1,y1,s1,x2,y2,s2, ) many plots in one
    command
  • Plot(x) where x is a matrix, will result in
    plotting each column as a separate trace

7
Line/Scatter
  • Plot a and then plot b
  • What do you see?
  • Only b
  • Matlab will replace the current plot with any new
    one unless you specifically tell it not to
  • To have both plots on one figure use the hold on
    command
  • To make multiple plots use the figure command

Make two plots plot(t,a) figure plot(t,b)
Put a and b on one plot plot(t,a) hold
on plot(t,b)
plot(t,a) plot(t,b)
8
Hold On / Hold Off
  • Hold on command only needs to be issued once per
    figure, thus calling hold on will result in all
    subsequent plot commands going to one figure
    unless a new figure command is issued or hold off
    is used.

plot(a) Hold on plot(b) plot(c) Hold
off Figure plot(d)
plot(a) Hold on plot(b) plot(c) Hold
off plot(d)
plot(a) Hold on plot(b) plot(c) Figure plot(d
)
9
Linespec
  • Currently, all the plots were done in the Matlab
    default color blue
  • This and many other features can be changed by
    selecting a different option within the plot
    command

red line Plot(t,a,r) Hold on black
line Plot(t,b,k) green dots Plot(t,c,g.)
cyan xs Plot(t,d,cx) dashed magenta line
with os Plot(t,e,--om)
10
Linespec
  • Now we have added color, line style and markers
    to the data
  • We can also modify line width, marker edge and
    fill color and marker size

dashed magenta line with os,
Plot(t,e,--om, LineWidth,3,'MarkerEdgeColor'
,k, 'MarkerFaceColor',y, MarkerSize,9)
11
Labels, Title and Legend
  • To add labels to the x and y axes, we can use the
    xlabel and ylabel commands
  • To add a title, use the title command
  • To add a legend, use the legend command

plot(t,a,t,b,r,t,c,--om) generate all
plots in one shot title(Random
Plots) xlabel(time(ms)) ylabel(f(t)) legend(
Function 1,Function 2,Function 3)
12
Quick Assignment 1
  • Plot a as a thick black line
  • Plot b as a series of red circles.
  • Label each axis, add a title and a legend

13
Quick Assignment 1
figure plot(t,a,'k','LineWidth',3) hold
on plot(t,b,'ro') xlabel('Time
(ms)') ylabel('f(t)') legend('t2','sin(2pit)'
) title('Mini Assignment 1')
14
Axis commands
  • We can alter the displayed range and other
    parameters of the plot by using the axis command
  • Axis(xmin xmax ymin ymax)
  • Axis equal
  • Axis square

Figure Plot(t,a,r) Axis(-2 2 -2 2)
15
Error Bars
  • In addition to markers, matlab can generate error
    bars for each datapoint using the errorbar
    command
  • errorbar(x,y,e) or errorbar(x,y,ll,lu)
  • Will generate line plus error bars at y/-e or
    (y-ll,ylu)

t2 0110 f t2(rand(1,length(t2))-0.5) err1
0.1f err2_l 0.1f err2_u
0.25f errorbar(t2,f,err1) figure errorbar(t2,f
,err2_l, err2_u)
16
Subplots
  • So far we have only been generating individual
    plots
  • Matlab can also generate a matrix of plots using
    the subplot command

figure subplot(2,2,1) plot(t,a) subplot(2,2,2) p
lot(t,b) subplot(2,2,3) plot(t,c) subplot(2,2,4)
plot(t,d)
17
Quick Assignment 2
  • Generate a 3x1 array of figures, each with a
    title
  • Axis range of plots 1 and 2 should be 0 to 1 on x
    and y
  • Plot 1 should include function a and b (color
    code)
  • Plot 2 should include c and d (color code)
  • Plot 3 should include f with error bars of your
    liking

18
Quick Assignment 2
figure subplot(3,1,1) plot(t,a,t,b,'r') axis(0
1 0 1) title('Functions a and
b') subplot(3,1,2) plot(t,c,t,d,'m') axis(0 1 0
1) title('Functions c and d') subplot(3,1,3) err
orbar(t2,f,err1) title('function f with
errorbars')
19
Bar Graphs
  • So far we have focused on line/scatter plots, the
    other most common plot type is a bar graph
  • Matlab bar(x,y,width) command will generate a bar
    graph that is related to the underlying functions
    where the width of each bar is specified by width
    (default 1)
  • Can be used with subplot and all the other
    features we have discussed so far

t3 0125 f sqrt(t3) bar(t3,f)
20
Histograms
  • Matlab hist(y,m) command will generate a
    frequency histogram of vector y distributed among
    m bins
  • Also can use hist(y,x) where x is a vector
    defining the bin centers
  • Note you can use histc function if you want to
    define bin edges instead
  • Can be used with subplot and all the other
    features we have discussed so far

hist(b,10) Figure Hist(b,-1 -0.75 0 0.25 0.5
0.75 1)
21
Quick Assignment 3
  • Generate the following data set
  • It results in multiple noisy repeats of some
    trial
  • Create mean_x to be the mean of all the trials at
    each point of x
  • Create std_x to be the standard deviation of all
    trials at each point of x
  • Add labels and titles

t 00.11 for(i125) x(i,) exp(-10.t)
0.5(rand(1,length(t))-0.5) end
22
Quick Assignment 3
  • Make a plot that is to include 2 subplots
  • Plot 1
  • Plot each individual trial (25 lines) in thin
    dotted black lines,
  • tip remember about how matlab interprets
    matricies for the plot command.
  • Plot the mean of the trials with a thick, red,
    dashed line and error lines surrounding each
    datapoint that correspond to the standard
    deviation of each of the points
  • Plot 2
  • A histogram that expresses the distribution of
    the signal at the end of each trial (last sample)

23
Quick Assignment 3
t 00.11 for(i125) x(i,) exp(-10.t)
0.5(rand(1,length(t))-0.5) end mean_x
mean(x) std_x std(x) figure subplot(2,1,1) plo
t(t,x,'k') hold on errorbar(t,mean_x,std_x,'--r'
,'LineWidth', 3) title('Repeats of a Given
Task') xlabel('Repeat') ylabel('Error Rate')
subplot(2,1,2) hist(x(,11),10) title('Distributi
on of Endpoint Position') xlabel('Deviation
(mm)') ylabel('Occurances')
24
Quick Assignment 3
25
Recap
  • Learned about basic 2D plot functions
  • plot,bar,hist
  • Dealing with multiple plots and graphs
  • Hold, figure, subplot
  • Discussed adding features to graphs
  • Xlabel,title,legend,

26
3D Plots
  • Matlab provides a wide range of 3D plot options,
    we will talk about 3 different plot types.
  • Mesh, Surf, Contour

27
Dataset
  • Try x,y,z peaks(25)
  • Does this work?
  • If not, lets create an arbitrary dataset
  • x-1010 y -1010 zx.2y.2

28
Mesh
  • Connects a series of discrete data points with a
    mesh
  • mesh(x,y,z) where X(i) and Y(j) are the
    intersections of the mesh and Z(i,j) is the
    height at that intersection
  • mesh(Z) assumes X and Y are simple 1..N and 1..M

29
Surf
  • Very similar to mesh, with filled grid

30
Contour
  • Projection of equal heights of 3D plot onto a 2D
    plane
  • Use like surf or mesh contour(x,y,z)

31
Meshc,surfc
  • Combination surface/mesh and contour plot
  • Use like surf or mesh meshc(x,y,z)

32
Plot3
  • Plots lines and points in space, just like plot
    but now for an (x,y,z) triple
  • Plot3(x,y,z), each a vector of equal length
  • Use all the same specfiers to generate colors,
    line-types, etc.

33
Plot commands
  • Note that almost all 2D plotting commands such as
    xlabel, ylabel, title will still work in 3D plots
  • 3D plots can be added as subplots just like any
    other plot type
  • Some other special features and functions exist

34
View
  • Changes the view of the current 3D plot
  • view(az,el)
  • Az azimuth (rotation around z-axis)
  • El Elevation (rotation around xy-plane)
  • Useful rotations (0,0),(90,0)

35
Colorbar
  • For the 3D plots, it is useful to add a colorbar
    to the figure to indicate height.
  • Colorbar(vert) adds a vertical colorbar
  • Colorbar(horiz) adds a horizontal colorbar

36
caxis
  • Allows you to set the range of colors in the
    plot.
  • Caxis(min max) will choose the range of colors
    from the current color map to assign to the plot.
    Values outside this range will use the min/max
    available

37
Colormap
  • Plenty of other controls to personalize the plot
  • colormap colormap_name sets to new map
  • Colormap (sets the types of colors that appear in
    3D plot). These can be custom of one of several
    premade
  • bone Hsv, jet, autumn, vga, summer,
  • See help graph3D for more
  • Use colormapeditor function for graphical
    selection of color map

38
Colormap
39
Next Week
  • Basic Stats / Basic Matlab wrap up

40
Getting Help
  • Help and Documentation
  • Digital
  • Accessible Help from the Matlab Start Menu
  • Updated online help from the Matlab Mathworks
    website
  • http//www.mathworks.com/access/helpdesk/help/tech
    doc/matlab.html
  • Matlab command prompt function lookup
  • Built in Demos
  • Websites
  • Hard Copy
  • Books, Guides, Reference
  • The Student Edition of Matlab pub. Mathworks Inc.
Write a Comment
User Comments (0)
About PowerShow.com