Matlab Graphics 2-D
Matlab Graphics 2-D
FIGURE WINDOWS
MATLAB directs graphics output to a window called figure that is separate from the command window. The figure function creates figure windows. Example: >>figure
2-D Plotting
The plot function is used to produce twodimensional curves, using x- and y-data matrices specified by the user. plot(xdata,ydata,clm) You can plot multiple lines at once, using pairs of x- and y-data, or triples of x, y. plot(x1,y1,clm1,x2,y2,clm2)
Adding a Grid
GRID ON creates a grid on the current figure GRID OFF turns off the grid from the current figure GRID toggles the grid state
>> plot(x,y,r)
>> grid on
Try to graph the following using the different line styles, markers and colors.
1. Graph y = 2cos3x 2. Graph the exponential function, logarithmic function, inverse trigonometric function, hyperbolic function with appropriate domain.
HOLD ON holds the current plot HOLD OFF releases hold on the current plot
HOLD toggles the hold state
>> x = 0:0.1:2*pi;
>> y = sin(x);
>> plot(x,y,r) >> grid on >> hold on >> plot(x, exp(-x),b:*)
GRAPH ANNOTATIONS
Example Type the following commands in the command window. >>x=0:pi/20:2*pi;
>>y=sin(x);
>>plot(x,y,bs-,linewidth,2) >>hold on
>>y1=cos(x);
>>plot(x,y1,r>:,linewidth,2)
(ii) Enter the text of the label and click anywhere in the figure background to close the text entry box.
SAVING FIGURES
You can save figures with Save or Save As through the File menu on the Figure Window. This will create a .fig file.
2. >> subplot(2,3,1) >> subplot(2,3,2) >> subplot(2,3,3) >> subplot(2,3,4) >> subplot(2,3,5) >> subplot(2,3,6)
subplot(2,2,i) where i = 1 to 4
subplot(2,3,i) where i = 1 to 6
ezplot
ezplot is an easy to use function plotter for algebraic and transcendental functions, parametric equations, implicit and explicit functions. ezplot(f) plots the expression f = f(x) over the default domain -2 < x < ezplot(f,[a,b]) plots f = f(x) over a < x < b
Examples: >> subplot(2,3,1) >> ezplot(cos(x)) >> subplot(2,3,2) >> ezplot(cos(x),[0, pi]) >> subplot(2,3,3) >> ezplot(1/y-log(y)+log(-1+y)+x-1) >> subplot(2,3,4) >> ezplot(x^2+y^2-4) >> subplot(2,3,5) >> ezplot(x^3+y^3-5*x*y+1/5,[-3,3]) >> subplot(2,3,6) >> ezplot(sin(t),cos(t))
POLAR CURVES
Polar in polar coordinates can be created using the polar(t,r,S) function, where t is the angle vector in radians, r is the radius vector, and S is an optional character string describing the color, marker symbol, and/or line style.
HISTOGRAM
Histogram illustrates the distribution of values in a vector. hist(y) draws a 10-bin histogram for the data in vector y. hist(y,n), where n is a scalar, draws a histogram with n bins. hist(y,x), where x is a vector, draws a histogram using the bins specified in x.
Example >>x=-2.9:0.2:2.9; %specify the bins to use >>y=randn(5000,1); %generate 5000 random points >>hist(y,x) %draw the histogram >>title(Histogram of Gaussian Data)
PIE CHART
Standard pie charts can be created using the pie(a,b) function, where a is a vector of values and b is an optional logical vectors describing a slice or slices to be pulled out of the pie chart. The pie3 function renders the pie chart with a 3-D appearance.
Example >>a=[0.5 1 1.6 1.2 0.8 2.1]; >>subplot(1,2,1) >>pie(a,a==max(a)); %produces chart a and pull out the biggest slice. >>subplot(1,2,2) >>explode=[1 0 0 0 0 0 ]; >>pie(a,explode) % Which part is pulled out?
BAR GRAPHS
Bar graphs display vector or matrix data. By default, a bar graph represents each element in matrix. Bars in a 2-D graph, created by bar function, are distributed along the x-axis with each element in a column drawn at a different location. All elements in a row are clustered around the same location on the x-axis.
Example
>> bar(y)
>> subplot(1,2,2) >>bar3(y)
area
stem3
ezsurf
ezsurf(f) creates a graph of f(x,y), where f is a string that represents a mathematical function of two variables, such as x and y.
Example: >> subplot(1,2,1) >> ezsurf('x^2+y^2') >> subplot(1,2,2) >> ezsurf('x^2-y^2')
ezmesh
ezmesh(f) creates a graph of f(x,y), where f is a symbolic expression that represents a mathematical function of two variables, such as x and y.