Download presentation
Presentation is loading. Please wait.
Published byHope Walker Modified over 9 years ago
1
1 "A picture is worth a thousand words." Graphical representation is useful for: Error detection - you can locate outliers in a dataset, program bugs, monitor numerical error growth. Analysis - explore the data, help find relationships among variables, patterns, suggest new hypotheses. Communication and presentation of results; visualization of a concept.
2
2 lRecognize the various types of plots that Matlab can create. lCreate simple 2D plots lAdd labels, title, and annotations to a plot. lGraph several variables on the same plot or produce multiple subplots on a page lSave your plots in various formats or print. Objectives You should be able to:
3
3 lgraph types - line, 2-D, 3-D, histogram, surface, contour, etc. lcolor, line style, marker style lplot combinations (several plots on the same axis or different axes but grouped together) Plotting and Graphics One of MATLAB’s strengths is its graphing capability. The user can specify
4
4 Figure Windows The Figure Window is designed to display data. Unlike the command window, it cannot accept text commands. You can explicitly open a figure window by typing in the command window >> figure(1) To close this window, either 1) click on the square (with an ) in the upper right corner or 2) type >> delete(1) 1 is an index to this particular window.
5
5 X-Y Plotting There are four types of X-Y plots, as shown below. Here, plot is the “standard” way to plot data. loglog provides a log scale on both the x and y axis. The arguments for all four specifications are the same. Graph Paper plotlinear x-y plot loglogloglog x-y plot semilogxsemi-log x-y plot (x-axis logarithmic) semilogysemi-log x-y plot (y-axis logarithmic)
6
6 Basic Plotting One of the simplest plots can be achieved with a single instruction, as follows. >> Y = [0.48.85 1.91.6.14]; >> plot(Y)
7
7 Basic Plotting (cont’d) To add a title, axis labels, and a grid, type >>title(‘Plot1’) >>xlabel(‘The X-Axis’) >>ylabel(‘The Y-Axis’) >>grid Semi-colons here do nothing. Semi-colons can be used to end a line, as can comma or space.
8
8 Basic Plotting (cont’d) Two vectors of the same length can be plotted against each other using the plot(X,Y), where the X vector is plotted on the abscissa (horizontal) and the Y vector is plotted on the ordinate (vertically). >> t = 0:.05: 4*pi; >> y = sin(t); >>plot(t,y) This produces a vector of values from 0 to 4*pi separated by.05. This produces a vector of values related by the sin function to t.
9
9 Basic Plotting (cont’d) Curve ends at 4*pi.
10
10 Basic Plotting (cont’d) Note that MATLAB scales the axes so that the data fits within the boundaries. The tick marks are placed evenly on the axis, and extra space is placed at the end when data does not fall on a tick mark (observe the right side of the plot). The number of tick marks can be changed by changing the size of the window – a smaller window produces fewer tick marks.
11
11 Basic Plotting (cont’d) More than one curve can be plotted on the same plot, using multiple arguments in the plot command. For example, consider >>f = 0:10; w = 2*f; v =.5*f.^2; >> plot(f,w,f,v) w is a linear function of f, and v is a quadratic function of f. Here, both are plotted verses f. f is repeated.
12
12 Basic Plotting (cont’d) Notice that MATLAB chooses different line colors.
13
13 Basic Plotting (cont’d) In this last example, two functions (vectors) of y were plotted against the same function (vector) of x. That is, the vector associated with the x- axis is the same for both functions. However, different vectors may be chosen. It is important to note that, for every x-y pair, the vectors must be the same length.
14
14 Basic Plotting (cont’d) If you ask MATLAB to plot data (using plot ) specified as a 2-dimensional matrix, it will plot each column as a separate curve. On the other hand, a row vector is converted into a column vector, and the resulting sequence of values is plotted. You can plot a row vector against a column vector, as long as they both have the same dimensions.
15
15 Basic Plotting (cont’d) If you specify plot(Y ), where Y is a 2- dimensional matrix, then MATLAB will plot each column in Y as a separate curve verses its position in the matrix. If you specify plot(x,Y), where x is a vector, then MATLAB will plot each column in Y as a separate curve versus x. >> x = (-5: 0.2 : 5)’; >>Y = [x.5*x.^2 (x.^3)/6]; Necessary so Y can be represented as 3 functions.
16
16 Basic Plotting (cont’d) >> plot(Y); grid Applies gridlines. Note that a ‘;’ separates the two commands. A comma would also work.
17
17 Basic Plotting (cont’d) >> plot(x,Y), grid Note change in x-axis.
18
18 Basic Plotting (cont’d) >> plot(x, Y), grid is an abbreviated version of >> plot(x, Y(:,1), x, Y(:,2), x, Y(:,3)), grid The latter may be useful if you want to plot certain columns but not others.
19
19 Line and Mark Styles You can specify the line type, the data mark, and colors of curves. Line types and data marks are shown below. Line Types Point Types Solid-Point. Dashed--Plus+ Dotted:Star* Dashdot-.Circleo x –markx
20
20 Line and Mark Styles (cont’d) >>x = 1:10; >>w = 2*x - 1; >>plot(x,w,’x’) Notice that the line is missing.
21
21 Line and Mark Styles (cont’d) Notice that MATLAB forms a plot by connecting data marks with straight lines. Therefore, smoother plots are achieved with more data points. Line colors can be be chosen using letters, as shown on the next slide. You can also access any of 16 colors using the format. >>plot(x,Y,’c#’) where # is an integer between 0 and 15. Thus, >>plot(x, Y, ‘r’) produces a solid red line, while
22
22 Line and Mark Styles (cont’d) >>plot(x, Y, ‘+g’) produces green ‘+’ symbols. Colors Yellowy Magentam Cyanc Redr Greeng Blueb Whitew Blackk Invisiblei The order of the two symbols + and g is not important. +g is equivalent to g+.
23
23 Complex Data Representation When arguments of the plot command are complex, only the real part is plotted. If there is only one complex argument, plot produces a plot of the real verses the imaginary part. >> t = linspace(1,10, 50); x = linspace(0, 6*pi, 50); >> z = exp(-t).*(sin(x) + 1i*cos(x)); >> plot(z) >> xlabel(‘Real Part’) >> ylabel(‘Imaginary Part’) Recall: 1i assures that MATLAB views i as regardless of a prior redefinition; e.g. i = 2.
24
24 Complex Data Representation (cont’d)
25
25 Polar Plots The polar command allows you to plot in polar coordinates. For example, you can plot antenna radiation patterns. You invoke the polar command with polar(theta,rho) where the angle vector is theta, and the magnitude vector is rho. You can plot a complex number as >>t=linspace(1,10,50); x=linspace(0,6*pi,50); >>z = exp(-t).*(sin(x) + 1i*cos(x)); >>polar(angle(z),abs(z))
26
26 Polar Plots (cont’d)
27
27 Plot Annotation and Legends (cont’d) MATLAB allows you to mark a special data point, such as a maximum value. So, for example, >>[m,i] = max(z); >>text(real(z(i)), imag(z(i)), ’Max_Value’) inserts ‘ Max_Value’, when this is applied after >> t=linspace(1,10,50); x=linspace(0, 6*pi, 50); >> z=exp(-t).*(sin(x)+1i*cos(x)); >> plot(z) Here, m returns the max value of z, while, i is its index.
28
28 Figure Window Control MATLAB can plot two or more graphs in the same Figure Window using the subplot command. Specifically, subplot(m,n,p), specifies the Figure Window to be divided into m rows, n columns, with the current plot indexed by p. For example, we can plot two plots one above the other with >>x = linspace(0, 2*pi,20); >>subplot(2,1,1), plot(x) >>subplot(2,1,2), plot(-2*x)
29
29 Figure Window Control (cont’d)
30
30 Figure Window Control (cont’d) Suppose you want to produce several figures in one program. You can either create the first figure and then clear it with: >> clf Or, you may just open a new figure window: >> figure(2) If you want to go back to working on figure (1), simply type >> figure(1) And commands entered after that will apply to figure 1.
31
31 Figure Window Control (cont’d) >>subplot(1,1,1) >>subplot(2,2,1), plot(x) >>subplot(2,2,2), plot(-2*x) >>subplot(2,2,3), plot(sin(x)) >>subplot(2,2,4), plot(cos(2*x)) >>subplot(2,2,3), title(‘Plot’), grid produces the figure on the next slide. Note the title specification at the end. Note that anything you type here like xlabel(‘x axis’) affects only subplot(2,2,3).
32
32 Figure Window Control (cont’d)
33
33 Figure Window Control (cont’d) A useful MATLAB command is the hold command. This prevents MATLAB from writing over a previously established Figure Window with a newly specified one. By typing hold on, you can plot a new curve on the same graph as previously plotted curves. To revert back to the default condition, type hold off. In this case, new plot function replaces the old Figure Window, erasing previous plots. Typing hold just causes the hold on and hold off to toggle back and forth.
34
34 Manual Axis Scaling MATLAB allows you to override the automatic axis scaling features and to set the plotting limits. 1.axis([XMIN, XMAX, YMIN,YMAX]) - sets axis limits for 2- D plots. 2. axis(‘auto’) – returns axis to autoscaling mode (antidote for axis(axis). 3. axis(‘equal’) – sets the axis so that tick marks on the x and y axis are equal. 4. axis(‘normal) – sets the axis back to unequal tick marks (antidote for axis(‘equal’). 5. axis(‘off’) - removes the axis, tick marks, and numbers from the plot.
35
35 Manual Axis Scaling (cont’d) Example: You can force the axis of some figure to be the same as some other figure, as follows. >>figure(1) >>A = axis; >>figure(2) >>axis(A) This can also be used with subplot functions. Without the “;”, MATLAB would print out the axis limits - horizontal and vertical.
36
36 Hardcopy You can obtain a hardcopy of the Figure Window by clicking on the File and then Print at the Figure Window. In the window that appears, choose which printer you want and click OK. Alternatively, you can just type print in the command window. This will cause your Figure Window to print to the default printer.
37
37 Saving and exporting figures You can choose to save the plot in another format such as jpg, tif, eps, or bmp by using “Export” from the File menu in the Figure window. For plots that you want to save in Matlab and annotate later, you can “Save as” a.fig file.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.