introduction to m atlab basic graphics

35
Introduction to MATLAB Basic Graphics www.opencadd.com.b r

Post on 21-Dec-2015

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to M ATLAB Basic Graphics

Introduction to MATLAB

Basic Graphics

www.opencadd.com.br

Page 2: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 2Introduction to MATLAB

Section Outline

• 2-D plotting

• Graph Annotation

• Subplots & Alternative Axes

• 3-D plotting

• Specialized plotting routines

• Patches & Images

• Saving & Exporting Figures

• Introduction to Handle Graphics

Ref: Color, Linestyle, Marker options Special characters using LaTeX

Page 3: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 3Introduction to MATLAB

2-D Plotting

• Specify x-data and/or y-data

• Specify color, line style and marker symbol(Default values used if ‘clmnot specified)

• Syntax:

• Plotting single line:

• Plotting multiple lines:plot(x1, y1, 'clm1', x2, y2, 'clm2', ...)

plot(xdata, ydata, 'color_linestyle_marker')

Ref: Color, Linestyle, Marker options

Page 4: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 4Introduction to MATLAB

2-D Plotting - exampleCreate a Blue Sine Wave

» x = 0:.1:2*pi;

» y = sin(x);

» plot(x,y)

» x = 0:.1:2*pi;

» y = sin(x);

» plot(x,y)

»plot_2d

Page 5: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 5Introduction to MATLAB

Adding a Grid

• GRID ON creates a grid on the current figure

• GRID OFF turns off the grid from the current figure

• GRID toggles the grid state

»grid on

Page 6: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 6Introduction to MATLAB

Adding additional plots to a figure

• HOLD ON holds the current plot

• HOLD OFF releases hold on current plot

• HOLD toggles the hold state

»addgraph

» x = 0:.1:2*pi;

» y = sin(x);

» plot(x,y,'b')

» grid on

» hold on

» plot(x,exp(-x),'r:*')

» x = 0:.1:2*pi;

» y = sin(x);

» plot(x,y,'b')

» grid on

» hold on

» plot(x,exp(-x),'r:*')

Page 7: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 7Introduction to MATLAB

Controlling viewing area

• ZOOM ON allows user to select viewing area

• ZOOM OFF prevents zooming operations

• ZOOM toggles the zoom state

• AXIS sets axis range

[xmin xmax ymin ymax]

» axis([0 2*pi 0 1])» axis([0 2*pi 0 1])

Page 8: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 8Introduction to MATLAB

Graph Annotation

TITLE

TEXTor

GTEXT

XLABEL

YLABEL

»annotation

LEGEND

Page 9: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 9Introduction to MATLAB

Plot Editor

»plotedit

Right click (Ctrl-LFT): on graphics objects to modify properties

• Enable Plotting Editing

• Add Text

• Add Arrow

• Add Line

• Zoom In

• Zoom Out

• Rotate 3D

• [Edit] -> Copy Figure / Options

Page 10: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 10Introduction to MATLAB

Using LaTeX in Graph Annotations

Font Type (applies inside {} or until changed):

• \fontname{ } AND \fontsize{ }

Appearance (applies inside {} or until removed):

• \bf boldface

• \it OR \sl italics OR slanted

• \rm remove text formatting (normal)

Subscript “_” or superscript “^”:

Applies to next character or {text in curly braces}

Greek letters and Symbols (prefix with “\”):

Selected symbols, e.g. '\pi' = »latex_exampRef: Special characters using LaTeX

Page 11: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 11Introduction to MATLAB

Exercise: 2-D Plotting

Create the following graph:

fontsize (14)

sin(10t)

cos(10t)

Courier New + Bold

Page 12: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 12Introduction to MATLAB

Solution: 2-D Plotting

» t = 0:0.01:0.5;» plot(t,sin(10*pi*t),'g-*', ... t,cos(10*pi*t),'k:o')» title(['\fontsize{14}In-Phase ({\itsolid})', ... 'and Quadrature ({\itdotted}) Signals'])» xlabel('\fontname{Courier New}\bfTime (\mus)')» ylabel('{\it''Normalized''} Signals');» text(0.2, cos(2*pi)+0.1, '\leftarrow----\rightarrow');» text(0.175, cos(2*pi)+0.2, '^\pi/_2 phase lag'); » axis([0 0.5 -1.5 1.5]);

% NOTE: Could have also used GTEXT or PLOTEDIT:% =============================================% gtext('\leftarrow----\rightarrow');% gtext('^\pi/_2 phase lag');

» t = 0:0.01:0.5;» plot(t,sin(10*pi*t),'g-*', ... t,cos(10*pi*t),'k:o')» title(['\fontsize{14}In-Phase ({\itsolid})', ... 'and Quadrature ({\itdotted}) Signals'])» xlabel('\fontname{Courier New}\bfTime (\mus)')» ylabel('{\it''Normalized''} Signals');» text(0.2, cos(2*pi)+0.1, '\leftarrow----\rightarrow');» text(0.175, cos(2*pi)+0.2, '^\pi/_2 phase lag'); » axis([0 0.5 -1.5 1.5]);

% NOTE: Could have also used GTEXT or PLOTEDIT:% =============================================% gtext('\leftarrow----\rightarrow');% gtext('^\pi/_2 phase lag');

»plot2d_soln

Page 13: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 13Introduction to MATLAB

SubplotsSUBPLOT- display multiple axes in the same figure window

»subplot(2,2,1);

»plot(1:10)

»subplot(2,2,2)

»x = 0:.1:2*pi;

»plot(x,sin(x))

»subplot(2,2,3)

»x = 0:.1:2*pi;

»plot(x,exp(-x),’r’)

»subplot(2,2,4)

»plot(peaks)

»subplot(2,2,1);

»plot(1:10)

»subplot(2,2,2)

»x = 0:.1:2*pi;

»plot(x,sin(x))

»subplot(2,2,3)

»x = 0:.1:2*pi;

»plot(x,exp(-x),’r’)

»subplot(2,2,4)

»plot(peaks)»subplotex

subplot(#rows, #cols, index)

Page 14: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 14Introduction to MATLAB

Alternative Scales for Axes

SEMILOGYlog Ylinear X

PLOTYY2 sets oflinear axes

LOGLOGBoth axes logarithmic

SEMILOGXlog Xlinear Y

»other_axes

Page 15: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 15Introduction to MATLAB

3-D Line Plotting

Ref: Color, Linestyle, Marker options

» z = 0:0.1:40;

» x = cos(z);

» y = sin(z);

» plot3(x,y,z)

» z = 0:0.1:40;

» x = cos(z);

» y = sin(z);

» plot3(x,y,z)

plot3(xdata, ydata, zdata, 'clm', ...)

»plot_3d

Page 16: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 16Introduction to MATLAB

3-D Surface Plotting

»surf_3d

Page 17: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 17Introduction to MATLAB

Exercise: 3-D Plotting

• Data from a water jet experiment suggests the following non-linear model for the 2-D stress in the cantilever beam’s horizontal plane.

where: = localized planar stress [MPa]

x = distance from end of beam [10-1m]

y = distance from centerline of beam [10-1m]

• For the particular setup used:(x = {0 to 6}, y = {-3 to 3}, = = = 1, = -0.2)

• Plot the resulting stress distribution

= e-x[sin(x)*cos(y)]x

Page 18: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 18Introduction to MATLAB

Solution: 3-D Plotting

» B = -0.2;

» x = 0:0.1:2*pi;

» y = -pi/2:0.1:pi/2;

» [x,y] = meshgrid(x,y);

» z = exp(B*x).*sin(x).*cos(y);

» surf(x,y,z)

» B = -0.2;

» x = 0:0.1:2*pi;

» y = -pi/2:0.1:pi/2;

» [x,y] = meshgrid(x,y);

» z = exp(B*x).*sin(x).*cos(y);

» surf(x,y,z)

»plot3d_soln

Page 19: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 19Introduction to MATLAB

Specialized Plotting Routines

»spec_plots

Page 20: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 20Introduction to MATLAB

Specialized Plotting Routines (2)

»spec_plots2

Page 21: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 21Introduction to MATLAB

» a = magic(4)

a =

16 2 3 13

5 11 10 8

9 7 6 12

4 14 15 1

» image(a);

» map = hsv(16)

map =

1.0000 0 0

1.0000 0.3750 0

1.0000 0.7500 0 .....

» colormap(map)

» a = magic(4)

a =

16 2 3 13

5 11 10 8

9 7 6 12

4 14 15 1

» image(a);

» map = hsv(16)

map =

1.0000 0 0

1.0000 0.3750 0

1.0000 0.7500 0 .....

» colormap(map)

Images Reduced Memory Requirements:Images represented as UINT8 - 1 byte

»imagex

Use Row 2 of colormap for pixel (1,2)

Row 2

Page 22: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 22Introduction to MATLAB

Example: Images

» load cape

» image(X)

» colormap(map)

» load cape

» image(X)

» colormap(map)

Page 23: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 23Introduction to MATLAB

Saving Figures

2 files created:• .m - text file• .mat - binary data

file.

»plot3d_soln

Page 24: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 24Introduction to MATLAB

• using the Dialog Box:File Menu / Print... >>printdlg

• from Command Line:

(Switches are optional)

• Controlling Page Layout:File Menu / Page Position>>pagedlg

Printing Figures

print -devicetype -options

Page 25: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 25Introduction to MATLAB

• Printing image to a file:

• Print Dialog Box: (File / Print...) >>printdlg

• Command Line:

(Switches are optional)

• Copying to Clipboard:

• Options: (File / Preferences)

• Copying: (Edit / Copy Figure)

Exporting Figures

print -devicetype -options filename

Page 26: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 26Introduction to MATLAB

Introduction to Handle Graphics

• Graphics in MATLAB consist of objects

• Every graphics objects has a unique handle and a set of properties which define it’s appearance.

• Objects are arranged in terms of a set hierarchy

Uicontrol

Image Line Patch Surface Text Light

Axes Uimenu

Figure

Root (Screen)

Page 27: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 27Introduction to MATLAB

Hierarchy of Graphics Objects Rootobject

Figureobject

UIControlobjects

UIMenuobjects

Axes object

Figureobject

Surfaceobject

Lineobjects

Textobjects

UIControlobjects

UIMenuobjects

Axes object

Figureobject

Page 28: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 28Introduction to MATLAB

1. Upon Creation

2. Utility Functions

0 - root object handle

gcf - current figure handle

gca - current axis handle

gco - current object handle

3. FINDOBJ

Obtaining an Object’s Handle

h_obj = findobj(h_parent, 'Property', 'Value', ...)

h_line = plot(x_data, y_data, ...)

What is the current object? • Last object created

• OR• Last object clicked

Default = 0 (root object)

Page 29: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 29Introduction to MATLAB

Deleting Objects - DELETE

» h = findobj('Color', [0 0 1])

» delete(h)

» h = findobj('Color', [0 0 1])

» delete(h)

delete(h_object)

» addgraph

Page 30: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 30Introduction to MATLAB

Modifying Object PropertiesUsing GET & SET

• Obtaining a list of current properties:

• Obtaining a list of settable properties:

• Modifying an object’s properties:

get(h_object)

set(h_object)

set(h_object, 'PropertyName', 'New_Value', ...)

Ref: HELPDESK - Handle Graphics Objects:\help\techdoc\infotool\hgprop\doc_frame.html

Page 31: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 31Introduction to MATLAB

Modifying Object PropertiesUsing the Property Editor

Property List:List of properties & current values for selected object

»propedit

Object Browser:Hierarchical list of graphics objects

Property / Value Fields:Selected property & current value (modify here)

Page 32: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 32Introduction to MATLAB

Modifying Object PropertiesUsing the RIGHT CLICK

Page 33: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 33Introduction to MATLAB

Working with Defaults - setting

• Most properties have pre-defined 'factory' values (Used whenever property values are not specified.)

• You can define your own 'default' values to be used for creating new objects.(Put default settings in “startup.m” to apply to whole session)

Syntax:set(ancestor,'Default<Object><Property>',<Property_Val>)

Use root object (0) to apply to all new objects

Page 34: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 34Introduction to MATLAB

» set(0, 'DefaultSurfaceEdgeColor', 'b')

» h=surf(peaks(15));

» set(0, 'DefaultSurfaceEdgeColor', 'b')

» h=surf(peaks(15));

Set the Default Surface EdgeColor to Blue & create new surface.

» set(h, 'EdgeColor', 'g')» set(h, 'EdgeColor', 'g')

Set the EdgeColor to GreenGreen

» set(h, 'EdgeColor', 'default')» set(h, 'EdgeColor', 'default')

specifies Default value

Reset back to Default Value

» set(h, 'EdgeColor', 'factory')» set(h, 'EdgeColor', 'factory')

specifies Factory value

Reset back to Factory Value

Example: Working with Defaults

»defaults

Page 35: Introduction to M ATLAB Basic Graphics

Copyright 1984 - 1998 by The MathWorks, Inc.

Basic Graphics - 35Introduction to MATLAB

Return Default Property Value to Factory Setting:

Create a new surface:

Working with Defaults - removing

» set(gcf, 'DefaultSurfaceEdgeColor', 'factory') » set(gcf, 'DefaultSurfaceEdgeColor', 'factory')

» set(gcf, 'DefaultSurfaceEdgeColor', 'remove') » set(gcf, 'DefaultSurfaceEdgeColor', 'remove')

» h = surf(peaks(15)); » h = surf(peaks(15));

»defaults

OR