Introduction to MATLAB 5: Graphics with MATLAB
Transcript of Introduction to MATLAB 5: Graphics with MATLAB
Introduction to MATLAB
5: Graphics with MATLAB
Georgios Georgiou
Department of Mathematics and Statistics
University of Cyprus
plot
plot
title
xlabel
ylabel
legend
text
grid
figure
plotedit
hold on, hold off
axis
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!
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)).
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)
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)
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)
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
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)
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=
−
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
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
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
=
=
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
>> 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
>> 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
>> 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
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)
>> 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
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
Logarithmic plots
plot(x,y)
loglog(x,y)
semilogx(y)
semilogy(x,y)
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
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
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
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
Multiple plots
subplot(m,n,p)
subplot(2,2,4)
2
3 4
subplot(2,2,1)
1
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
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
= +
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
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
Bar and area graphs
bar(x)
barh(x)
bar3(x)
bar3h(x)
area(x)
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
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])
Pie charts
>> x=[194.8,266.5,330.9];
>> pie(x)
25%
34%
42%
>> pie3(x)
42%
34%
25%
Histograms
>> y=randn(10000,1);
>> hist(y)
>> grid
-4 -3 -2 -1 0 1 2 3 40
500
1000
1500
2000
2500
3000
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=
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!
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
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
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− −=
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
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
Η εντολή
>> 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
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
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])
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
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
-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
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
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.
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!
Thank you!!