Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

IT

Workshop
Graphics
Types of MATLAB Plots (2-D and 3-D Plots)
Line Plots Vector Fields

Scatter and Bubble Charts Surface and Mesh Plots

Data Distribution Plots Volume Visualization

Discrete Data Plots Animation

Geographic Plots Images

Polar Plots

Contour Plots More examples:


https://in.mathworks.com/products/matlab/plot-gallery.html
Line Plots
x = 0:pi/100:2*pi;

y = sin(x);

plot(x,y)
Subplot
subplot(2,1,1);

x = linspace(0,10);

y1 = sin(x);

plot(x,y1)

subplot(2,1,2);

y2 = sin(5*x);

plot(x,y2)
Multiple Line Plots
x = linspace(-2*pi,2*pi);

y1 = sin(x);

y2 = cos(x);

figure

plot(x,y1,x,y2)
Using matrix to Plot
Y = magic(4)

figure

plot(Y)
Specifying Line Style
x = 0:pi/100:2*pi;

y1 = sin(x);

y2 = sin(x-0.25);

y3 = sin(x-0.5);

figure

plot(x,y1,x,y2,'--',x,y3,':')
Specifying Line Style, Color, and Marker
x = 0:pi/10:2*pi;

y1 = sin(x);

y2 = sin(x-0.25);

y3 = sin(x-0.5);

figure

plot(x,y1,'g',x,y2,'b--o',x,y3,'c*')
Markers at Specific Data Points
x = linspace(0,10);

y = sin(x);

plot(x,y,'-o','MarkerIndices',1:5:length(y))
Specifying Line Width, Marker Size, and
Marker Color
x = -pi:pi/10:pi;

y = tan(sin(x)) - sin(tan(x));

figure

plot(x,y,'--gs',...

'LineWidth',2,...

'MarkerSize',10,...

'MarkerEdgeColor','b',...

'MarkerFaceColor',[0.5,0.5,0.5])
Add Title and Axis Labels
x = linspace(0,10,150);

y = cos(5*x);

figure

plot(x,y,'Color',[0,0.7,0.9])

title('2-D Line Plot')

xlabel('x')

ylabel('cos(5x)')
Polynomial Curve Fitting
x = [1 2 3 4 5];

y = [5.5 43.1 128 290.7 498.4];

p = polyfit(x,y,3)

x2 = 1:.1:5;

y2 = polyval(p,x2);

plot(x,y,'o',x2,y2)

grid on

s = sprintf('y = (%.1f) x^3 + (%.1f) x^2 + (%.1f) x +


(%.1f)',p(1),p(2),p(3),p(4));

text(2,400,s)
Interpolation
x = 0:pi/4:2*pi;

v = sin(x);

xq = 0:pi/16:2*pi;

figure

vq1 = interp1(x,v,xq);

plot(x,v,'o',xq,vq1,':.');

xlim([0 2*pi]);

title('(Default) Linear Interpolation');


Plot 3D
t = 0:pi/50:10*pi;

st = sin(t);

ct = cos(t);

plot3(st,ct,t)
Meshgrid in Plotting
x = -2:0.25:2;

y = x;

[X,Y] = meshgrid(x);

F = X.*exp(-X.^2-Y.^2);

surf(X,Y,F)
Mesh Plot
[X,Y] = meshgrid(-8:.5:8);

R = sqrt(X.^2 + Y.^2) + eps;

Z = sin(R)./R;

mesh(X,Y,Z)
Surface Plot
[X,Y] = meshgrid(1:0.5:10,1:20);

Z = sin(X) + cos(Y);

C = X.*Y;

surf(X,Y,Z,C)

colorbar
Histogram
x = randn(10000,1);

nbins = 25;

h = histogram(x,nbins)
Scatter Plot
x = linspace(0,3*pi,200);

y = cos(x) + rand(1,200);

scatter(x,y)
Bar Plot
x = 1900:10:2000;

y = [75 91 105 123.5 131

150 179 203 226 249 281.5];

bar(x,y)

You might also like