Вы находитесь на странице: 1из 35

# Introduction to MATLAB

Basic Graphics

Introduction to MATLAB

Basic Graphics - 2

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

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 3

2-D Plotting
Specify x-data and/or y-data
Specify color, line style and marker symbol
(Default values used if clmnot specified)

Syntax:
single line:
Plotting
plot(xdata, ydata,

'color_linestyle_marker')

Plotting
lines:x2,
plot(x1,multiple
y1, 'clm1',

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 4

## 2-D Plotting - example

Create a Blue Sine Wave
xx == 0:.1:2*pi;
0:.1:2*pi;
yy == sin(x);
sin(x);
plot(x,y)
plot(x,y)

plot_2d

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 5

## GRID ON creates a grid

on the current figure

## GRID OFF turns off the

grid from the current
figure

state

grid on

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 6

current plot

## HOLD OFF releases

hold on current plot

## HOLD toggles the hold

state

xx == 0:.1:2*pi;
0:.1:2*pi;
yy == sin(x);
sin(x);
plot(x,y,'b')
plot(x,y,'b')
grid
grid on
on
hold
hold on
on
plot(x,exp(-x),'r:*')
plot(x,exp(-x),'r:*')

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 7

## ZOOM ON allows user

to select viewing area

## ZOOM OFF prevents

zooming operations

zoom state

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

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 8

Graph Annotation
LEGEND

TITLE

TEXT
or
GTEXT

YLABEL

XLABEL
annotation

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 9

Plot Editor

## Enable Plotting Editing

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

Zoom In
Zoom Out
Rotate 3D

plotedit

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 10

## 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_examp
Ref: Special characters using LaTeX

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 11

## Exercise: 2-D Plotting

Create the following graph:
fontsize (14)

sin(10t)

cos(10t)

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 12

## Solution: 2-D Plotting

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

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 13

Subplots
SUBPLOT- display multiple axes in the same figure window
subplot(#rows, #cols, index)
subplot(2,2,1);
subplot(2,2,1);
plot(1:10)
plot(1:10)
subplot(2,2,2)
subplot(2,2,2)
xx == 0:.1:2*pi;
0:.1:2*pi;
plot(x,sin(x))
plot(x,sin(x))
subplot(2,2,3)
subplot(2,2,3)
xx == 0:.1:2*pi;
0:.1:2*pi;

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

subplotex

subplot(2,2,4)
subplot(2,2,4)
plot(peaks)
plot(peaks)

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 14

LOGLOG
Both axes
logarithmic

SEMILOGY
log Y
linear X

SEMILOGX
log X
linear Y

PLOTYY
2 sets of
linear axes

other_axes

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 15

## 3-D Line Plotting

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

zz == 0:0.1:40;
0:0.1:40;
xx == cos(z);
cos(z);
yy == sin(z);
sin(z);
plot3(x,y,z)
plot3(x,y,z)

plot_3d
Ref: Color, Linestyle, Marker options

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 16

surf_3d

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 17

## Data from a water jet experiment suggests the

following non-linear model for the 2-D stress in
the cantilever beams horizontal plane.

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

## 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

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 18

## Solution: 3-D Plotting

BB == -0.2;
-0.2;
xx == 0:0.1:2*pi;
0:0.1:2*pi;
yy == -pi/2:0.1:pi/2;
-pi/2:0.1:pi/2;
[x,y]
[x,y] == meshgrid(x,y);
meshgrid(x,y);
zz == exp(B*x).*sin(x).*cos(y);
exp(B*x).*sin(x).*cos(y);
surf(x,y,z)
surf(x,y,z)

plot3d_soln

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 19

spec_plots

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 20

spec_plots2

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Images

Basic Graphics - 21

## Reduced Memory Requirements:

Images represented as UINT8 - 1 byte

aa == magic(4)
magic(4)
aa ==

Use Row 2
of colormap
for pixel (1,2)
16
16 22 33 13
13
55 11
11 10
10 88
99 77 66 12
12
44 14
14 15
15 11

image(a);
image(a);
map
map == hsv(16)
hsv(16)
map
map ==
1.0000
1.0000

00
1.0000
1.0000 0.3750
0.3750
1.0000
1.0000 0.7500
0.7500

00
00 Row 2
00 .....
.....

colormap(map)
colormap(map)
imagex

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 22

Example: Images

cape
image(X)
image(X)
colormap(map)
colormap(map)

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 23

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

plot3d_soln

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 24

Printing Figures

>>printdlg

## print -devicetype -options

(Switches are optional)

>>pagedlg

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 25

Exporting Figures

Command Line:

>>printdlg

## print -devicetype -options filename

(Switches are optional)

Copying to Clipboard:

## Options: (File / Preferences)

Copying: (Edit / Copy Figure)

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 26

## Every graphics objects has a unique handle and

a set of properties which define its appearance.
Root (Screen)

Figure

Uicontrol

Image

Line

Axes

Patch

Surface

Text

Light

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 27

Root
object
Figure
object

objects

UIControl
objects

Axes object

Surface
object
Line
objects

Text
objects

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 28

## Obtaining an Objects Handle

1. Upon Creation
h_line = plot(x_data, y_data, ...)

2. Utility Functions
0 - root object handle
gcf - current figure handle
gca - current axis handle
gco - current object handle

3. FINDOBJ
h_obj = findobj(h_parent,

## What is the current object?

Last object created
OR
Last object clicked

## 'Property', 'Value', ...)

Default = 0 (root object)

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 29

## Deleting Objects - DELETE

delete(h_object)

hh == findobj('Color',
findobj('Color', [0
[0 00 1])
1])
delete(h)
delete(h)

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 30

Using GET & SET

get(h_object)

set(h_object)

## Modifying an objects properties:

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

## Ref: HELPDESK - Handle Graphics Objects:

\help\techdoc\infotool\hgprop\doc_frame.html

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 31

## Modifying Object Properties

Using the Property Editor

Object Browser:
Hierarchical list of
graphics objects

## Property / Value Fields:

Selected property &
current value (modify here)

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

propedit

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 32

## Modifying Object Properties

Using the RIGHT CLICK

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 33

## 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

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 34

## Example: Working with Defaults

Set the Default Surface EdgeColor to Blue & create new
surface.
set(0,
set(0, 'DefaultSurfaceEdgeColor',
'DefaultSurfaceEdgeColor', 'b')
'b')
h=surf(peaks(15));
h=surf(peaks(15));

## Set the EdgeColor to Green

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

## Reset back to Default Value

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

## Reset back to Factory Value

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

defaults

## 1984 - 1998 by The MathWorks, Inc.

Introduction to MATLAB

Basic Graphics - 35

## Working with Defaults - removing

Return Default Property Value to Factory Setting:
OR

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

## Create a new surface:

hh == surf(peaks(15));
surf(peaks(15));

defaults