Sunteți pe pagina 1din 30

Graphics and visualization

Handle graphics functions


Figure window creation and control Axis creation and control Handle Graphics objects. Handle Graphics operations.

Plot function
Plot(a,b) -Plot vectors- plot two vectors a and b To plot x^3 for the interval[-2,2] X=-2:.5:2 Y=x.^3 Axis off or axis equal Title(frapgh of y=x^3 is) Xlabel(text) Ylabel(text) TEXT(X,Y,'string') gtext({'Firstline','Secondline'},'FontName','Times', 'Fontsize',12)

Plot miscellaneous
Figure Hold Cla=Clear current axis. Clf=Clear current figure. RESET(GCA) resets the properties of the current axis. Gca, Get handle to current axis Gcf -Get handle to current figure

V = GET(H,'PropertyName') set(gcf,'DefaultTextColor','red') X=-1.5:.05:1.5 Y=exp(-x^2)

To plot a graph of a function over a rectangular domain we need to create a grid of points

Specialized plotting

Text Annotations
plot(0:pi/20:2*pi,sin(0:pi/20:2*pi)) text(pi,0,' \leftarrow sin(\pi)','FontSize',18) annotate the point at (pi,0) with the string sin()

Using a Mathematical Expression to Title a Graph


This example uses TeX character sequences to create graph labels. The following statements add a title and x- and y-axis labels to an existing graph. title('{\itAe}^{-\alpha\itt}sin\beta{\itt} \alpha<<\beta') xlabel('Time \musec.') ylabel('Amplitude')

Numeric Variables
You can specify numeric variables in text strings using the num2str (number to string) function. x = 21; ['Today is the ',num2str(x),'st day.'] text(xcoord,ycoord,['Today is the ',num2str(x),'st day.'])

Multiline Text
str1(1) = {'Center each line in the Uicontrol'}; str1(2) = {'Also check out the textwrap function'}; text(5.75,sin(2.5),str1,'HorizontalAlignment' ,'right')

Bar Graphs
A bar chart displays the values in a vector or matrix as horizontal or vertical bars. Y = [5 2 1 873 986 555 4 3 2]; bar(Y)

Pie Chart
Pie charts display the percentage that each element in a vector or matrix contributes to the sum of all elements. Eg X contains yearly sales figures for a specific product over a five-year period,(every 4 months) X = [19.3 22.1 51.6; 34.2 70.3 82.4; 61.4 82.9 90.8; 50.5 54.9 59.1; 29.4 36.3 47.0];

x = sum(X); First, create a vector containing zeros. explode = zeros(size(x)); Then find the slice that contributes the most and set the corresponding explode element to 1. [c,offset] = max(x); explode(offset) = 1; h = pie(x,explode); colormap summer

Histogram and ROSE


Generate a bell-curve histogram from Gaussian data. x = -2.9:0.1:2.9; y = randn(10000,1); hist(y,x) Create a rose plot showing the distribution of 50 random numbers. theta = 2*pi*rand(1,50); rose(theta)

Stem plots
A stem plot displays data as lines (stems) terminated with a marker symbol at each data value. In a 2-D graph, stems extend from the xaxis. The stem function displays two-dimensional discrete sequence data. Eg alpha = .02; beta = .5; t = 0:4:200; y = exp(-alpha*t).*sin(beta*t); plot(t,y) stem(t,y)

3-D Stem Plot of an FFT


For example, fast Fourier transforms are calculated at points around the unit circle on the complex plane. So, it is interesting to visualize the plot around the unit circle. Calculating the unit circle th = (0:127)/128*2*pi; x = cos(th); y = sin(th); f = abs(fft(ones(10,1),128)); stem3(x,y,f','d','fill') view([-65 30])

Stairstep plots display data as the leading edges of a constant interval (i.e., zero-order hold state). This type of plot holds the data at a constant yvalue for all values between x(i) and x(i+1), where i is the index into the x data. This type of plot is useful for drawing time-history plots of digitally sampled data systems. alpha = 0.01;beta = 0.5;t = 0:10; f = exp(-alpha*t).*sin(beta*t); stairs(t,f) hold on plot(t,f,'--*') hold off

Stairstep Plots

Direction and Velocity Vector Graphs


A compass plot displays direction or velocity vectors as arrows emanating from the origin. X, Y, and Z are in Cartesian coordinates and plotted on a circular grid.Draw a compass plot of the eigenvalues of a matrix. Z = eig(randn(20,20)); compass(Z)

Feather plot
Create a feather plot showing the direction of theta. theta = (-90:10:90)*pi/180; r = 2*ones(size(theta)); [u,v] = pol2cart(theta,r); feather(u,v);

Quiver
A quiver plot displays velocity vectors as arrows with components (U,V) at the points (X,Y). Expanding X and Y CoordinatesMATLAB expandes X and Y, if they are not matrices. This expansion is equivalent to calling meshgrid to generate matrices from vectors: [x,y] = meshgrid(-2:.2:2,-1:.15:1); z = x .* exp(-x.^2 - y.^2); [px,py] = gradient(z,.2,.15); contour(x,y,z), hold on quiver(x,y,px,py), hold off, axis image

A contour plot displays isolines of matrix Z. Label the contour lines using clabel.The clabel function adds height labels to a twodimensional contour plot. Command contour(u) [x,y]=meshgrid(-2.1:0.15:2.1,-6:0.15:6) u=80*y.^2.*exp(-x.^2-0.3*y.^2); contour(u)

3D plots
t = 0:pi/50:10*pi; plot3(sin(t),cos(t),t) axis square; grid on

Mesh and surface


[x,y,z]=meshgrid(-2:0.5:2,-2:0.5:2,5) Mesh(z) Surf(z) [x,y]=meshgrid(-2:0.5:2,-2:0.5:2) z=2*x+3 Mesh(z) Surf(z)

Representing a Matrix as a Surface


MATLAB can create different forms of surface plots. Mesh plots are wire-frame surfaces that color only the lines connecting the defining points. Surface plots display both the connecting lines and the faces of the surface in color.

3D Contour plot
It can be drawn under a surface with command meshc or surfc [x,y]=meshgrid(-2:.2:2); z=x.^exp(-x.^2-y.^2); meshc(z) figure surfc(z)

Visualization of matrices
Mesh function can be used to visualize matrices a=zeroes(30,30); a(:,15)=0.2*ones(30,1); a(7,:)=0.1*ones(1,30); a(15,15)=1; mesh(a)

Rotation of 3D graphs
for az=-37.5:15:-37.5+360 mesh(a),view(az,el) %default el=30 pause(0.5) end

Ribbon plot
[x,y]=meshgrid(-8:1:8); r=sqrt(x.^2+y.^2) + eps; z=sin(r)./r; ribbon(z)

Waterfall
[x,y]=meshgrid(-2:0.1:2); z=x.*exp(-x.^2-y.^2) waterfall(z)

S-ar putea să vă placă și