Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recap Chapter 5 “Plotting” Two Dimensional Plots Simple x-y Plots Titles, Labels and Grids Multiple Plots.

Similar presentations


Presentation on theme: "Recap Chapter 5 “Plotting” Two Dimensional Plots Simple x-y Plots Titles, Labels and Grids Multiple Plots."— Presentation transcript:

1

2 Recap Chapter 5 “Plotting” Two Dimensional Plots Simple x-y Plots Titles, Labels and Grids Multiple Plots

3 Plots with More than One Line A plot with more than one line can be created in several ways By default, the execution of a second plot statement will erase the first plot However, you can layer plots on top of one another by using the hold on command Execute the following statements to create a plot with both functions plotted on the same graph x = 0:pi/100:2*pi; y1 = cos(x*4); plot(x,y1) y2 = sin(x); hold on; plot(x, y2)

4 The hold on command can be used to layer plots on same graph

5 Continued…. Semicolons are optional on both the plot statement and the hold on statement MATLAB will continue to layer the plots until the hold off command is executed: hold off Another way to create a graph with multiple lines is to request both lines in a single plot command MATLAB interprets the input to plot as alternating x and y vectors, as in plot(X1, Y1, X2, Y2) where the variables X1, Y1 form an ordered set of values to be plotted and X2, Y2 form a second ordered set of values.

6 Continued…. If the plot function is called with a single matrix argument, MATLAB draws a separate line for each column of the matrix The x -axis is labeled with the row index vector, 1: k, where k is the number of rows in the matrix. This produces an evenly spaced plot, sometimes called a line plot If plot is called with two arguments, one a vector and the other a matrix, MATLAB successively plots a line for each row in the matrix For example: we can combine y1 and y2 into a single matrix and plot against x : Y = [y1; y2]; plot(x,Y) This creates the same plot as previous figure, with each line a different color

7 Continued…. Here’s another more complicated example: X = 0:pi/100:2*pi; Y1 = cos(X)*2; Y2 = cos(X)*3; Y3 = cos(X)*4; Y4 = cos(X)*5; Z = [Y1; Y2; Y3; Y4]; plot(X, Y1, X, Y2, X, Y3, X, Y4) This code produces the same result as plot(X, Z)

8 Multiple Plots on Same Graph

9 Continued…. A function of two variables, the peaks function produces sample data that are useful for demonstrating certain graphing functions Calling peaks with a single argument n will create an nxn matrix We can use peaks to demonstrate the power of using a matrix argument in the plot function The command plot(peaks(100)) results in the impressive graph

10 The peak Function, plotted with single augment in plot Command

11 Plots of Complex Arrays If the input to the plot command is a single array of complex numbers, MATLAB plots the real component on the x -axis and the imaginary component on the y -axis For example: if A = [0+0i,1+2i, 2+5i, 3+4i] then plot(A) title('Plot of a Single Complex Array') xlabel('Real Component') ylabel('Imaginary Component') returns graph shown in next slide

12

13 Continued…. If we attempt to use two arrays of complex numbers in the plot function, the imaginary components are ignored The real portion of the first array is used for the x -values, and the real portion of the second array is used for the y –values To illustrate, first create another array called B by taking the sine of the complex array A : B = sin(A) returns B = 0 3.1658 + 1.9596i 67.4789-30.8794i 3.8537 -27.0168i and plot(A,B) title('Plot of Two Complex Arrays') xlabel('Real Component of the X array') ylabel('Real Component of the Y array') gives us an error statement. Warning: Imaginary parts of complex X and/or Y arguments ignored The data are still plotted, as shown in figure in next slide

14

15 Line, Color and Mark Style The appearance of plots can be changed by selecting user- defined line styles and line colors and by choosing to show the data points on the graph with user specified mark styles The command help plot returns a list of the available options

16

17 Continued…. The following commands illustrate the use of line, color, and mark styles: x = [1:10]; y = [58.5, 63.8, 64.2, 67.3, 71.5, 88.3, 90.1, 90.6, 89.5,90.4]; plot(x,y,':ok') The resulting plot (shown in next slide) consists of a dashed line, together with data points marked with circles

18

19 Continued…. To specify line, mark, and color styles for multiple lines, add a string containing the choices after each pair of data points If the string is not included, the defaults are used For example: plot(x,y,':ok',x,y*2,'--xr',x,y/2,'-b') results in the graph shown in next slide

20

21 Axis Scaling and Annotating Plots MATLAB automatically selects appropriate x -axis and y -axis scaling Sometimes, it is useful for the user to be able to control the scaling Control is accomplished with the axis function Executing the axis function without any input axis freezes the scaling of the plot If you use the hold on command to add a second line to your graph, the scaling cannot change To return control of the scaling to MATLAB, simply re-execute the axis function

22 Continued…. The axis function also accepts input defining the x -axis and y - axis scaling The argument is a single matrix, with four values representing: The minimum x value shown on the x -axis The maximum x value shown on the x -axis The minimum y value shown on the y -axis The maximum y value shown on the y -axis Thus, the command axis([-2, 3, 0, 10]) fixes the plot axes to x from -2 to +3 and y from 0 to 10

23

24 Continued…. It is often useful to create plots where the scaling is the same on the x- and y -axis. This is accomplished with the command axis equal MATLAB offers several additional functions that allow to annotate plots The legend function requires the user to specify a legend in the form of a string for each line plotted, and displays it in the upper right hand corner of the plot The text function allows to add a text box to plot, which is useful for describing features on the graph It requires the user to specify the location of the lower left-hand corner of the box in the plot window as the first two input fields, with a string specifying the contents of the text box in the third input field

25 Continued…. The use of both legend and text is demonstrated in the following code (modification of last graph) legend('line 1', 'line 2', 'line3') text(1,100,'Label plots with the text command') We added a title, x and y labels, and adjusted the axis with the following commands: xlabel('My x label'), ylabel('My y label') title('Example graph for Chapter 5‘) axis([0,11,0,200]) The results are shown in next slide

26

27 Continued…. A function similar to text is gtext, which allows the user to interactively place a text box in an existing plot The gtext function requires a single input, the string to be displayed gtext('This string will display on the graph') Once executed, a crosshair appears on the graph The user positions the crosshair to the appropriate position The text is added to the graph when any key on the keyboard is depressed, or a mouse button is selected

28 Subplots The subplot command allows to subdivide the graphing window into a grid of m rows and n columns The function subplot(m,n,p) splits the figure into an mxn matrix The variable p identifies the portion of the window where the next plot will be drawn For example: if the command subplot(2,2,1) is used, the window is divided into two rows and two columns, and the plot is drawn in the upper left-hand window

29

30 Continued…. The following commands split the graph window into a top plot and a bottom plot: x = 0:pi/20:2*pi; subplot(2,1,1) plot(x,sin(x)) subplot(2,1,2) plot(x,sin(2*x) The first graph is drawn in the top window, since p=1 Then the subplot command is used again to draw the next graph in the bottom window

31

32 Polar Plots MATLAB provides plotting capability with polar coordinates: polar(theta, r) generates a polar plot of angle theta (in radians) and radial distance r For example: the code x = 0:pi/100:pi; y = sin(x); polar(x,y) generates the plot in figure in next slide A title was added in the usual way: title('The sine function plotted in polar coordinates is a circle.')

33

34 Logarithmic Plots For most plots that we generate, the x - and y -axes are divided into equally spaced intervals; these plots are called linear or rectangular plots Occasionally, however, we may want to use a logarithmic scale on one or both of the axes A logarithmic scale (to the base 10) is convenient when a variable ranges over many orders of magnitude, because the wide range of values can be graphed without compressing the smaller values Logarithmic plots are also useful for representing data that vary exponentially

35

36 Continued…. the logarithm of a negative number or of zero does not exist, if data include these values, MATLAB will issue a warning message and will not plot the points in question However, it will generate a plot based on the remaining points Each command for logarithmic plotting can be executed with one argument, as we saw in plot(y) for a linear plot In these cases, the plots are generated with the values of the indices of the vector y used as x values

37 Example

38

39 Bar Graphs and Pie Charts Bar graphs, histograms, and pie charts are popular forms for reporting data

40 Example clear, clc x = [1,2,5,4,8]; y = [x;1:5]; subplot(2,2,1) bar(x),title('A bar graph of vector x') subplot(2,2,2) bar(y),title('A bar graph of matrix y') subplot(2,2,3) bar3(y),title('A three-dimensional bar graph') subplot(2,2,4) pie(x),title('A pie chart of x')

41

42 Histograms A histogram is a special type of graph that is particularly useful for the statistical analysis of data It is a plot showing the distribution of a set of values In MATLAB, the histogram computes the number of values falling into 10 bins that are equally spaced between the minimum and maximum values

43 Example Define a matrix x as the set of grades from the Introduction to Engineering final, the scores could be represented in a histogram generated with the following code x = [100,95,74,87,22,78,34,35,93,88,86,42,55,48]; hist(x)

44

45 Example Continued…. The default number of bins is 10, but if we have a large data set, we may want to divide the data up into more bins For example, to create a histogram with 25 bins, the command would be hist(x, 25) If you set the hist function equal to a variable, as in A = hist(x) the data used in the plot are stored in A : A =1 2 1 1 1 0 1 1 3 3

46 X–Y Graphs with Two Y-Axes

47

48 Continued…. The plot of sin( x ) looks like it runs straight along the line x=0, because of the scale The plotyy function allows us to create a graph with two y -axes, the one on the left for the first set of ordered pairs and the one on the right for the second set of ordered pairs: subplot(2,1,2) plotyy(x,y1,x,y2) Titles and labels were added in the usual way The y -axis was not labeled, because the results are dimensionless

49 Continued…. The plotyy function can create a number of different types of plots by adding a string with the name of the plot type after the second set of ordered pairs The plots(shown in next slide) were created with the following code and have a logarithmically scaled axis: subplot(2,1,1) plotyy(x,y1,x,y2, 'semilogy') subplot(2,1,2) plotyy(x,y1,x,y2,'semilogx')

50

51 Continued…. For other problems it may be required to add y -axis labels. The left-hand y -axis is easy—just add the label in the usual way ylabel('Left y-axis label') The right-hand y -axis label is trickier You can add it using MATLAB’s interactive controls, described in a later section, or you can use handle graphics This involves giving the plot a name, and then using the name to switch to the second axis set The code is a = plotyy(x,y1,x,y2) ylabel(a(2),'Right y-axis label')


Download ppt "Recap Chapter 5 “Plotting” Two Dimensional Plots Simple x-y Plots Titles, Labels and Grids Multiple Plots."

Similar presentations


Ads by Google