0% found this document useful (0 votes)
3 views

3- MATLAB Graphics

Uploaded by

huyphan1455
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

3- MATLAB Graphics

Uploaded by

huyphan1455
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

MATLAB Graphics

BASIC 2-D GRAPHS


 figure()
plot(rand(1, 20))

 plot(y) x = 0:pi/40:4*pi;
plot(x, sin(x))

 plot(x, y)

 plot(x,y,’s ’)

 hold on/off

 grid on/off
BASIC 2-D GRAPHS
 Labels
figure
plot (x1,y1,x2,y2,x3,y3,…)
title(’text’)
xlabel(’horizontal’)
ylabel(’vertical’)
Legend (‘x_legend’, ‘y_legend’s)
hold on/off
grid on/off

x = linspace(-2*pi,2*pi,100);
y1 = sin(x);
y2 = cos(x);
figure; plot(x,y1,’red’);
hold on; plot (x,y2,’blue’);
title('Line Plot of Sine and Cosine Between -2\pi and 2\pi')
xlabel('-2\pi < x < 2\pi')
ylabel('Sine and Cosine Values')
legend('y = sin(x)','y = cos(x)','Location','southwest')
BASIC 2-D GRAPHS
 Multiple plots on the same axes
 Use hold
plot(x,y1)
hold on
plot(x,y2)
plot(x,y3)
 Plot concurrent

plot(x1, y1, x2, y2, x3, y3, ... )


x = linspace(0,10,50);
 Or use vector/matrix y1 = sin(x);
plot(x,y1)
title('Combine Plots')
plot(X, Y) hold on
Where X,Y: matries y2 = sin(x/2);
plot(x,y2)
y3 = 2*sin(x);
scatter(x,y3)
hold off
BASIC 2-D GRAPHS
 Line styles, markers and color

plot(x, y, ’--’)
plot(x, y, ’o’)
plot(x,sin(x), x, cos(x), ’om--’)

figure
t = 0:pi/20:2*pi;
plot(t,sin(t),'-.r*')
hold on
plot(t,sin(t-pi/2),'--mo')
plot(t,sin(t-pi),':bs')
hold off
Thêm chú thích cho đồ thị
Xác định các điểm mà cực đại
của tín hiệu này là biên độ = 0
của tín hiệu khác
Line styles, markers and color
Line Marker Description
Description
Style 'o' Circle
- Solid line '+' Plus sign
-- Dashed line '*' Asterisk
: Dotted line '.' Point
-. Dash-dot line 'x' Cross
'_' Horizontal line
'|' Vertical line
Color: 's' Square
'd' Diamond
c cyan '^' Upward-pointing triangle
m magenta 'v' Downward-pointing triangle
y yellow '>' Right-pointing triangle
k black '<' Left-pointing triangle
r red 'p' Pentagram
g green 'h' Hexagram
b blue
w white
BASIC 2-D GRAPHS
 Axis limits
axis( [xmin, xmax, ymin, ymax] )
axis auto
axis manual
axis tight
axis equal
axis normal
axis on/off

x = linspace(0,2*pi);
y = sin(x);
plot(x,y,'-o')
axis([0 2*pi -1.5 1.5])
BASIC 2-D GRAPHS
 Multiple plots in a figure: subplot Vẽ đồ thị của
y = sin(x)
subplot(2,2,1);
subplot(m, n, p) y = sin(3x)
plot(x, sin(x)) n y = sin(5x)
title()
xlabel
lên cùng 1 đồ thị
ylabel
3x1
axis
……
m p=1 p=2
subplot(2,2,2);
plot(x,sin(2*x)
….

subplot(2,2,3) p=3 p=4


plot(x,sin(3*x)

 Merge subplot

y1 = cos(x)
y2 = 1 – x^2/2 + x^4/4
y3 = y1 + y2
BASIC 2-D GRAPHS
 figure, clf and cla

figure(h): creates a new figure window, or makes


figure h the current figure
clf: clears the current figure window
cla: deletes all plots and text from the current axes
BASIC 2-D GRAPHS
 Graphical input

[x, y] = ginput

 A movable cross-hair appears on the graph. Clicking


saves its co-ordinates in x(i) and y(i). Pressing
Enter terminates the input.
BASIC 2-D GRAPHS
 Logarithmic plots

 E.g
semilogy(x, y)

x = 0:0.01:4;
semilogy(x, exp(x)), grid
BASIC 2-D GRAPHS
 Polar plots
x = r cos(θ),
y = r sin(θ),
and θ varies between 0 and 2π radians (360◦).
polar(theta, r)
e.g.

x = 0:pi/40:2*pi;
polar(x, sin(2*x)),grid
BASIC 2-D GRAPHS
 Plotting rapidly changing mathematical functions

x = 0.01:0.001:0.1;
plot(x, sin(1./x))

fplot(@(x) sin(1/x), [0.01 0.1]) % no, 1./x not needed!

y = sin (1/x)
BASIC 2-D GRAPHS
 The Property Editor
 The most general way of editing a graph is by using the
Property Editor, e.g.,

 Edit  Figure Properties


FREQUENCIES, BAR CHARTS AND
HISTOGRAMS
 Bar Chart (single column)
bar(y)
bar(x,y)
bar(___,width)
bar(___,style)
bar(___,color)

= [75 91 105 123.5 131 150 179 203 226 249 281.5];
ar(y)

= 1900:10:2000;
= [75 91 105 123.5 131 150 179 203 226 249 281.5];
ar(x,y)
Bar Charts
 Bar chart (multiple columns)
bar(y)
bar(x,y)
bar(___,width)
bar(___,style)
bar(___,color)

y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y)
Bar Charts
 Bar Chart (Stacked column)
bar(y)
bar(x,y)
bar(___,width)
bar(___,style)
bar(___,color)

y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y,'stacked')
Exercise
 Download:
 Library Data.xls
from FTP server: ftp.viethanit.edu.vn => folder:
Matlab => Data
user: sinhvien pass:123456

 Bar chart (single columns & multiple columns)


 Number of users access the library 2018, 2019, 2020
 Number of book loans 2018, 2019, 2020
 Number of access to the library website 2018, 2019,
2020
Histogram
 A histogram is an approximate representation of
the distribution of numerical data.
hist(x)
[h n] = hist(x)

x = randn(10000,1);
hist(x)
 Mean value
mean(X)

 Standard deviation

s = std(X)
Exercise
 Download:
 K20-Chuyên đề 2_CE_Matlab.xls
from VKU eLearning system

 Histogram
 Analyse the histogram of grades
3-D PLOTS
 plot3
plot3(x, y, z)
plot3(X,Y,Z)
plot3(X,Y,Z,s)
plot3(x1,y1,z1,s1,x2,y2,z2,s2,x3,y3,z3,s3,...)

−0.02 𝑡
𝑥=𝑒 sin ( 𝑡 )
−0.02 𝑡
y =𝑒 c 𝑜𝑠 ( 𝑡 ) 40

30
= 0:pi/50:10*pi; z-axis
ure 20

ot3(exp(-0.02*t).*sin(t), exp(-0.02*t).*cos(t),10t)
abel('x-axis'), ylabel('y-axis'), zlabel('z-axis')
0
d 1
0.5 1
0 0.5
0
-0.5 -0.5
y-axis -1 -1
x-axis
3-D PLOTS
 comet3
 comet3 is similar to plot3 except that it draws with a
moving ‘comet head’

t = 0:pi/50:10*pi;

comet3(exp(-0.02*t).*sin(t), exp(-0.02*t).*cos(t), t);

40

30

z-axis
20

10

0
1
0.5 1
0 0.5
0
-0.5 -0.5
y-axis -1 -1
x-axis
3-D PLOTS
 Mesh surfaces
mesh(X,Y,Z)
mesh(X,Y,Z,C)
z = x2 −y2
0 ≤ x ≤ 5, 0 ≤ y ≤ 5.
[x y] = meshgrid(0:5); 25

z = x.^2 - y.^2 20

mesh(x,y,z) 30 15
colorbar 20
10
10
5
0
0
What happenning? -10
-5
[x y] = meshgrid(0:0.25:5); -20
-10
-30
30 -15
30
20
20 -20
meshgrid: Cartesian grid in 2-D/3-D space 10
10
-25
0 0
3-D PLOTS
 Surf
 The function mesh draws a surface as a ‘wire frame’. An
alternative visualization is provided by surf, which
generates a faceted view of the surface (in color).
25

[x y] = meshgrid(0:5); 20
z = x.^2 - y.^2 30 15
surf(x,y,z) 20
10
colorbar 10
5
0
0
-10
-5
-20
-10
-30
30 -15
30
20
20 -20
10
10
-25
0 0
HANDLE GRAPHICS
 Getting handles
figure()
x = 0:pi/20:2*pi; hf
h_line = plot(x, sin(x))
hold on
h_axes
h_labelx = xlabel(’x’)
……..
set (h_line, ‘color’, ‘r’, ‘Linewidth’,2)
h_line, h_labelx

gcf gets the handle of the current figure, e.g.,


hf = gcf;
gca gets the handle of the current axes.
h_axes = gca;
gco gets the handle of the current graphics object
h_line = gco
 Setting handles
set(handle, ‘PropertyName’, PropertyValue)

set(h_line, ’linewidth’, 4);


set(hf,'color','blue')
 A vector of handles
 If a graphics object has a number of children the get
command used with the children property returns a
vector of the children’s handles
1

0.8
x = 0:pi/20:4*pi;
0.6
plot(x, sin(x))
hold on 0.4

plot(x, exp(-0.1*x).*sin(x), ’o’) 0.2

hold off 0

-0.2
% vector of the children’s handles -0.4
hkids = get(gca,’child’) -0.6
set(hkids(1), ’color’, ’red’)
-0.8
set(hkids(2), ’linestyle’, ‘- -’)
-1
0 2 4 6 8 10 12 14
Exercise
 Vẽ đồ thị hình sin và thay đổi màu sắc, tiêu đề của
đồ thị
EDITING PLOTS
 Plot edit mode
 Select Tools -> Edit Plot in the figure window or
Press Arrow icon

 Property Editor
 Double-click on an object, or
 Right-click on an object and select Properties
ANIMATION
 The comet and comet3 functions can be used to
draw comet plots.
 The getframe function may be used to generate
‘movie frames’ from a sequence of graphs. The
movie function can then be used to play back the
1

movie a specified number of times.


0.8

for k = 1:16 0.6

plot(fft(eye(k+16))) 0.4
axis equal 0.2
M(k) = getframe; 0
end
-0.2

-0.4

movie(M,5) -0.6

-0.8

-1
-1 -0.5 0 0.5 1
 Animation with Handle Graphics
animated sine graph the object must not be erased when it is
drawn again.
gure()
= 0;
= 0;
= pi/40;
= plot(x, y, ’o’, ’EraseMode’, ’none’); % ’xor’ shows only current point
’ none’ shows all points
is([0 20*pi -2 2])

r x = dx:dx:20*pi;
x = x + dx;
y = sin(x);
et(p, ’XData’, x, ’YData’, y)
drawnow
nd
COLOR
 Colormaps
 Colormaps define the color scheme for many types of
visualizations, such as surfaces parula
and patches
Parula colormap array
turbo Turbo colormap array
hsv HSV colormap array
hot Hot colormap array

[x y] = meshgrid(0:5); cool Cool colormap array

z = x.^2 - y.^2 spring Spring colormap array


summer Summer colormap array
surf(x,y,z)
autumn Autumn colormap array
colorbar winter Winter colormap array
gray Gray colormap array
colormap(‘cool’) bone Bone colormap array
copper Copper colormap array
pink Pink colormap array
lines Lines colormap array
jet Jet colormap array
colorcube Colorcube colormap array
prism Prism colormap array
flag Flag colormap array
 Color of surface plots
Color bar

6
10
z = peaks; 4
surf(z), colormap(jet), colorbar 5

2
0
%change colormap
0
colormap(prism) -5

-2
-10
60
60 -4
40
40
20 -6
20
0 0
LIGHTING AND CAMERA
 MATLAB uses lighting to add realism to graphics,
e.g., illuminating a surface by shining light on it
from a certain angle

z = peaks;
surf(z), colormap(jet), colorbar
camlight right

%change camlight
camlight left
SAVING, PRINTING AND EXPORTING
GRAPHS
 Saving
 Select: File  Save from the figure window.
 Command: saveas(H,'FILENAME','FORMAT')
Extensio
Resulting Format
saveas(gcf, 'output', 'fig') n
saveas(gcf, 'output', 'bmp') .fig MATLAB® FIG-file (invalid for Simulink block
diagrams)
Saveas(gcf, ’output.bmp’
.m MATLAB FIG-file and MATLAB code that opens
figure (invalid for Simulink block diagrams)
.jpg JPEG image
.png Portable Network Graphics
.eps EPS Level 3 Black and White
.pdf Portable Document Format
.bmp Windows® bitmap
.emf Enhanced metafile
.pbm Portable bitmap
.pcx Paintbrush 24-bit
.pgm Portable Graymap
.ppm Portable Pixmap
.tif TIFF image, compressed
x = [2 4 7 2 4 5 2 5 1 4];
bar(x);
saveas(gcf,'Barchart.png')
 Printing
 Select File  Print

 Exporting a graph
 export to the clipboard
 Edit  Copy Figure
 export a figure to a file in a specific graphics format
 File  Export
Exercise
 9.1
 the population of the USA from 1790 to 2000
 9.4
Bài tập về nhà
 Vẽ mũ Mê-hi-cô bằng MATLAB

You might also like