Introduction to MATLAB 5: Graphics with MATLAB

52
Introduction to MATLAB 5 : Graphics with MATLAB Georgios Georgiou Department of Mathematics and Statistics University of Cyprus

Transcript of Introduction to MATLAB 5: Graphics with MATLAB

Page 1: Introduction to MATLAB 5: Graphics with MATLAB

Introduction to MATLAB

5: Graphics with MATLAB

Georgios Georgiou

Department of Mathematics and Statistics

University of Cyprus

Page 2: Introduction to MATLAB 5: Graphics with MATLAB

plot

plot

title

xlabel

ylabel

legend

text

grid

figure

plotedit

hold on, hold off

axis

Page 3: Introduction to MATLAB 5: Graphics with MATLAB

plot

>> x=linspace(-2,2,101); plot(x, sin(x))

>> xlabel('x'), title('y=sin(x)')

>> grid

>> legend('sinx')

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

x

y=sin(x)

sinx

Much more options in the figure window!

Page 4: Introduction to MATLAB 5: Graphics with MATLAB

Plot with complex functions

>> t=0:pi/10:2*pi;

>> plot(exp(i*t),‘r-o')

>> axis equal

-1 -0.5 0 0.5 1-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

plot(Z) is equivalent to plot(real(Z), imag(Z)).

Page 5: Introduction to MATLAB 5: Graphics with MATLAB

ezplot

ezplot(f) plots f(x) in [-2π, 2π].

ezplot(f, a, b) or ezplot(f, [a, b]) plot f(x) in [a,b]

ezplot(f, [xmin xmax ymin ymax])

>> ezplot('exp(x)')

-4 -3 -2 -1 0 1 2 3 4 5 6

0

50

100

150

200

250

x

exp(x)

Page 6: Introduction to MATLAB 5: Graphics with MATLAB

Example 1

2

1( )

1f x

x=

+

>> ezplot('1./(1+x.^2)')

-5 0 5

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

x

1/(1+x2)

Page 7: Introduction to MATLAB 5: Graphics with MATLAB

Example 2

2

2( )

1

xf x

x=

>> f = @(x) x.^2./(x.^2-1);

>> ezplot(f)

-5 0 5

-1.5

-1

-0.5

0

0.5

1

1.5

2

2.5

3

x

x2/(x

2-1)

Page 8: Introduction to MATLAB 5: Graphics with MATLAB

ezplot: implicit functions

3 2 1( , ) 5 0

5f x y x y xy= + − + =

>> ezplot('x^3+y^2-5*x*y+1/5',[-3,3])

x

y

x3+y

2-5 x y+1/5 = 0

-3 -2 -1 0 1 2 3-3

-2

-1

0

1

2

3

Page 9: Introduction to MATLAB 5: Graphics with MATLAB

ezplot: parametric curves

( ), ( ), [ , ]x x t y y t t a b= =

>> ezplot('cos(t)', 'sin(t)',[0,2*pi])

ezplot(‘x(t)’, ’y(t)’, [a,b])

-1 -0.5 0 0.5 1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

x

y

x = cos(t), y = sin(t)

Page 10: Introduction to MATLAB 5: Graphics with MATLAB

fplotfplot(f, [xmin, xmax])

fplot(f, [xmin, xmax, ymin, ymax])

>> f = @(x) x.^2./(x.^2-1);

>> fplot(f, [-2,1])

-2 -1.5 -1 -0.5 0 0.5 1-400

-300

-200

-100

0

100

200

2

2( )

1

xf x

x=

Page 11: Introduction to MATLAB 5: Graphics with MATLAB

fplot

>> f = @(x) x.^2./(x.^2-1);

>> fplot(f,[-6 6 -1.5 3])

2

2( )

1

xf x

x=

-6 -4 -2 0 2 4 6-1.5

-1

-0.5

0

0.5

1

1.5

2

2.5

3

Page 12: Introduction to MATLAB 5: Graphics with MATLAB

fplot

2( ) sinf x x x=

>> fplot('x*sin(x)^2',[-3*pi, 3*pi]), title('x sin^2x')

-8 -6 -4 -2 0 2 4 6 8-8

-6

-4

-2

0

2

4

6

8x sin2x

Page 13: Introduction to MATLAB 5: Graphics with MATLAB

comet

>> t=0:pi/1000:8*pi;

>> x=sqrt(t/20).*cos(t); y=t.*sin(t)/20;

>> comet(x,y)

comet(x,y)

-1.5 -1 -0.5 0 0.5 1 1.5-1.5

-1

-0.5

0

0.5

1

1.5

>> plot(x,y), hold on, comet(x,y)

( ) cos20

sin( )

20

tx t t

t ty t

=

=

Page 14: Introduction to MATLAB 5: Graphics with MATLAB

Colors, lines, and symbols

[color] Color Color

b

g

r

c

m

y

k

w

blue

green

red

cyan

magenta

yellow

black

white

blue

green

red

cyan

magenda

yellow

black

άσπρο

plot(x,y, ' [color][stype][ltype]' ).

[ltype] Line type

-

:

--

-.

solid

dotted

dashed

dashdot

[stype] Symbol

.

o

x

+

*

s

d

v

^

<

>

p

h

point

circle

x-mark

plus

star

square

diamond

triangle (down)

triangle (up)

triangle (left)

triangle (right)

pentagram

hexagram

Page 15: Introduction to MATLAB 5: Graphics with MATLAB

>> x = -1:0.2:1;

>> y=exp(x);

>> plot(x, y)

or

>>plot(x, y, 'b- ')

-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 10

0.5

1

1.5

2

2.5

3

Plot

Page 16: Introduction to MATLAB 5: Graphics with MATLAB

>> plot(x, y, 'k--')

-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 10

0.5

1

1.5

2

2.5

3

Plot

Page 17: Introduction to MATLAB 5: Graphics with MATLAB

>> plot( x, y, 'rs')

-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 10

0.5

1

1.5

2

2.5

3

Plot

Page 18: Introduction to MATLAB 5: Graphics with MATLAB

Multiple graphs

plot( x1, y1, ' [colour][stype][ltype]', x2, y2, ' [colour][stype][ltype]', ……. )

legend ('legend y1', 'legend y2', ……)

>> x=0:0.02:2;

>> y=sin(x);

>> z=exp(x);

>> plot( x, y, 'r', x, z, '--')

>> grid

>> legend ( 'sin(x)', 'exp(x)' )

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20

1

2

3

4

5

6

7

8

sin(x)

exp(x)

Page 19: Introduction to MATLAB 5: Graphics with MATLAB

>> x=0:0.15:2;

>> y=exp(x);

>> plot( x, y, 'b', x, y, 'ro')

>> xlabel('x')

>> ylabel('e^x')

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 21

2

3

4

5

6

7

8

x

ex

Plot

Page 20: Introduction to MATLAB 5: Graphics with MATLAB

hold

>> t=linspace(-1,1);

>> y=t.^2 + 2*t -1;

>> plot(t,y)

>> hold on

>> z=cos(t)

>> plot(t,z)

>> hold off-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1

-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

Page 21: Introduction to MATLAB 5: Graphics with MATLAB

Logarithmic plots

plot(x,y)

loglog(x,y)

semilogx(y)

semilogy(x,y)

Page 22: Introduction to MATLAB 5: Graphics with MATLAB

plot51y x= +

>> x=0:0.1:100;

>> y=1+2*x.^5;

>> plot(x,y), grid, xlabel('x'), ylabel('y')

>> title('Linear/linear')

0 10 20 30 40 50 60 70 80 90 1000

0.5

1

1.5

2

2.5x 10

10

x

y

Linear/linear

Page 23: Introduction to MATLAB 5: Graphics with MATLAB

loglog(x,y)51y x= +

>> loglog(x,y), grid, xlabel('x'), ylabel('y')

>> title('Log/log')

10-1

100

101

102

100

102

104

106

108

1010

1012

x

y

Log/log

Page 24: Introduction to MATLAB 5: Graphics with MATLAB

semilogx(x,y)51y x= +

>> semilogx(x,y), grid, xlabel('x'), ylabel('y')

>> title('Log/linear')

10-1

100

101

102

0

0.5

1

1.5

2

2.5x 10

10

x

y

Log/linear

Page 25: Introduction to MATLAB 5: Graphics with MATLAB

semilogy(x,y)51y x= +

>> semilogy(x,y), grid, xlabel('x'), ylabel('y')

>> title('Linear/log')

0 10 20 30 40 50 60 70 80 90 10010

0

102

104

106

108

1010

1012

x

y

Linear/log

Page 26: Introduction to MATLAB 5: Graphics with MATLAB

Multiple plots

subplot(m,n,p)

subplot(2,2,4)

2

3 4

subplot(2,2,1)

1

Page 27: Introduction to MATLAB 5: Graphics with MATLAB

Example

>> t = -pi:2*pi/100:pi;

>>f1=sin(t.^2);

>>f2=(sin(t)).^2;

>>f3=cos(t.^2);

>>f4=(cos(t)).^2;

>>subplot(2,2,1);plot(t,f1);

>>title('sin(t^2)')

>>subplot(2,2,2);plot(t,f2);

>>title('sin(t)^2')

>>subplot(2,2,3);plot(t,f3);

>>title('cos(t^2)')

>>subplot(2,2,4);plot(t,f4);

>>title('cos(t)^2')

-4 -2 0 2 4-1

-0.5

0

0.5

1sin(t2)

-4 -2 0 2 40

0.2

0.4

0.6

0.8

1sin(t)2

-4 -2 0 2 4-1

-0.5

0

0.5

1cos(t2)

-4 -2 0 2 40

0.2

0.4

0.6

0.8

1cos(t)2

Page 28: Introduction to MATLAB 5: Graphics with MATLAB

Graphs in polar coordinates

>> t=0:0.01:2*pi;

>> r=3*cos(t/2).^2+t;

>> polar(t,r, 'm')

polar(theta,r)

2

4

6

8

10

30

210

60

240

90

270

120

300

150

330

180 0

23cos2

r

= +

Page 29: Introduction to MATLAB 5: Graphics with MATLAB

fill

>> x=linspace(0,1,1001); y=sqrt(1-x.^2);

>> fill(x,y,'b')

fill(X,Y,C) fills the 2-D polygon defined by vectors X and

Y with the color specified by C.

0 0.2 0.4 0.6 0.8 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Page 30: Introduction to MATLAB 5: Graphics with MATLAB

fill

>> x=linspace(0,1,1001); y=sqrt(1-x.^2);

>> X=[x 0]; Y=[y 0];

>> fill(X,Y,'m'), axis equal

0 0.2 0.4 0.6 0.8 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Page 31: Introduction to MATLAB 5: Graphics with MATLAB

Bar and area graphs

bar(x)

barh(x)

bar3(x)

bar3h(x)

area(x)

Page 32: Introduction to MATLAB 5: Graphics with MATLAB

Example 1

>> x = -2.9:0.2:2.9;

>> y=exp(-x.^2);

>> bar(x,y)

>> colormap cool

-3 -2 -1 0 1 2 30

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Page 33: Introduction to MATLAB 5: Graphics with MATLAB

Example 2>> x=[2 3 6 5 1];

>> subplot(2,2,1), bar(x), title('bar ([2 3 6 5 1])')

>> subplot(2,2,2), bar3(x), title('bar3 ([2 3 6 5 1])')

>> subplot(2,2,3), barh(x), title('barh ([2 3 6 5 1])')

>> subplot(2,2,4), bar3h(x), title('bar3h ([2 3 6 5 1])')

1 2 3 4 50

2

4

6bar ([2 3 6 5 1])

12

34

5

0

5

10

bar3 ([2 3 6 5 1])

0 2 4 6

1

2

3

4

5

barh ([2 3 6 5 1])

05

10

1

2

3

4

5

bar3h ([2 3 6 5 1])

Page 34: Introduction to MATLAB 5: Graphics with MATLAB

Pie charts

>> x=[194.8,266.5,330.9];

>> pie(x)

25%

34%

42%

>> pie3(x)

42%

34%

25%

Page 35: Introduction to MATLAB 5: Graphics with MATLAB

Histograms

>> y=randn(10000,1);

>> hist(y)

>> grid

-4 -3 -2 -1 0 1 2 3 40

500

1000

1500

2000

2500

3000

Page 36: Introduction to MATLAB 5: Graphics with MATLAB

3D plots

ezsurf

>> z = @(x,y) cos(x).*cos(y);

>> ezsurf(z)

-5

0

5

-5

0

5

-1

-0.5

0

0.5

1

x

cos(x) cos(y)

y

cos cosz x y=

Page 37: Introduction to MATLAB 5: Graphics with MATLAB

3D plots

meshgrid and surf

>> [x,y] = meshgrid(-2:0.1:2, -4:0.2:3);

>> z = x .* exp(-x.^2 - y.^2);

>> surf(x,y,z)

-2

-1

0

1

2

-4

-2

0

2

4-0.5

0

0.5

2 2x yz xe− −=

We can put axis labels!

Page 38: Introduction to MATLAB 5: Graphics with MATLAB

3D plots

surfc

>> [x,y] = meshgrid(-2:0.1:2, -4:0.2:3);

>> z = x .* exp(-x.^2 - y.^2);

>> surfc(x,y,z)

2 2x yz xe− −=

-2

-1

0

1

2

-4

-2

0

2

4-0.5

0

0.5

Page 39: Introduction to MATLAB 5: Graphics with MATLAB

3D plots

mesh

>> [x,y] = meshgrid(-2:0.1:2, -4:0.2:3);

>> z = x .* exp(-x.^2 - y.^2);

>> mesh(x,y,z)

2 2x yz xe− −=

-2

-1

0

1

2

-4

-2

0

2

4-0.5

0

0.5

Page 40: Introduction to MATLAB 5: Graphics with MATLAB

Contour plots

ezcontour

>> z = @(x,y) x .* exp(-x.^2 - y.^2);

>> ezcontour(z)

x

y

x exp(-x2 - y2)

-3 -2 -1 0 1 2 3-3

-2

-1

0

1

2

3

2 2x yz xe− −=

Page 41: Introduction to MATLAB 5: Graphics with MATLAB

Contour plots

ezcontourf

>> z = @(x,y) x .* exp(-x.^2 - y.^2);

>> ezcontourf(z)

2 2x yz xe− −=

x

y

x exp(-x2 - y2)

-3 -2 -1 0 1 2 3-3

-2

-1

0

1

2

3

Page 42: Introduction to MATLAB 5: Graphics with MATLAB

Contour plots

colorbar

>> z = @(x,y) x .* exp(-x.^2 - y.^2);

>> ezcontourf(z)

>> colorbar

2 2x yz xe− −=

x

y

x exp(-x2 - y2)

-3 -2 -1 0 1 2 3-3

-2

-1

0

1

2

3

-0.4

-0.3

-0.2

-0.1

0

0.1

0.2

0.3

0.4

Page 43: Introduction to MATLAB 5: Graphics with MATLAB

Η εντολή

>> colorbar

προσθέτει λεζάντα για το κάθε χρώμα, όπως φαίνεται πιο κάτω:

x

y

x exp(-x2 - y2)

-3 -2 -1 0 1 2 3-3

-2

-1

0

1

2

3

-0.4

-0.3

-0.2

-0.1

0

0.1

0.2

0.3

0.4

Page 44: Introduction to MATLAB 5: Graphics with MATLAB

Contour plots

contour, contourf

>> [x,y] = meshgrid(-2:0.1:2, -2:0.1:2);

>> z = x .* exp(-x.^2 - y.^2);

>> contourf(x,y,z), colorbar

2 2x yz xe− −=

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

-0.4

-0.3

-0.2

-0.1

0

0.1

0.2

0.3

0.4

Page 45: Introduction to MATLAB 5: Graphics with MATLAB

contour, contourf

contour(x,y,z,n)

contour(x,y,z,v)

contourf(x,y,z,v)

contour(x,y,z,[v v])

contourf(x,y,z,[v v])

Page 46: Introduction to MATLAB 5: Graphics with MATLAB

Example 1

>> [x,y]=meshgrid(-2:0.02:2,-2:0.02:2);

>> z=(x-x.*y+y.^2).*exp(-x.^2-y.^2);

>> contour(x,y,z,-.5:0.05:5,'Linewidth',2), colorbar

2 22( ) x yz x xy y e− −= − −

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

-0.4

-0.3

-0.2

-0.1

0

0.1

0.2

0.3

0.4

0.5

0.6

Page 47: Introduction to MATLAB 5: Graphics with MATLAB

Example 2

>> [x,y]=meshgrid(-2:0.02:2,-2:0.02:2);

>> z=(x-x.*y+y.^2).*exp(-x.^2-y.^2);

>> contour(x,y,z,[0 0]), colorbar

2 22( ) x yz x xy y e− −= − −

-2 -1 0 1 2-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

u=0

Page 48: Introduction to MATLAB 5: Graphics with MATLAB

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

Example 3

>> [x,y]=meshgrid(-2:0.02:2,-2:0.02:2);

>> z=(x-x.*y+y.^2).*exp(-x.^2-y.^2);

>> contourf(x,y,z,[0 0])

2 22( ) x yz x xy y e− −= − −u<0

Page 49: Introduction to MATLAB 5: Graphics with MATLAB

3D curves: plot3

t=0:pi/100:20*pi;

x=t.*cos(t);

y=t.^2.*sin(t);

z=sqrt(t);

plot3(x,y,z)

2( ) cos , ( ) sin , ( ) , [0,20 ]x t t t y t t t z t t t = = =

-100

-50

0

50

100

-4000

-2000

0

2000

40000

2

4

6

8

>> t=0:pi/100:20*pi;

>> x=t.*cos(t);

>> y=t.^2.*sin(t);

>> z=sqrt(t);

>> comet3(x,y,z)

comet3

Page 50: Introduction to MATLAB 5: Graphics with MATLAB

Storing a figure in a file: print

print -device -options filename

print –dps ‘foo’Save the current figure to a postscript file named 'foo.ps'

Examples

print -djpeg -r150 figuStore current figure into ‘figu.jpg’ with a 150 digit resolution

printSends the current figure to your current printer.

Page 51: Introduction to MATLAB 5: Graphics with MATLAB

Device options

print -depsc -tiff -r300 matildaSaves current figure at 300 dpi in color EPS to matilda.eps with a TIFF

preview

-dwinc : Send figure to current printer in color

-dmeta : Send figure to clipboard (or file) in Metafile format

-dpsc : PostScript for color printers

-dpsc2 : Level 2 PostScript for color printers

-depsc : Encapsulated Color PostScript

-depsc2 : Encapsulated Level 2 Color PostScript

-djpeg<nn> : JPEG image, quality level of nn

-dtiff : TIFF with packbits (lossless run-length encoding)

compression

See help print for more info!

Page 52: Introduction to MATLAB 5: Graphics with MATLAB

Thank you!!