xy Plotting Functions Matlab Help

xy Plotting Functions

The most common plot is the xy plot. Its name assumes that we are plotting a function y = I(x), although, of course, other symbols may be used. We plot the x values on the horizontal axis (the abscissa), and the y values on the vertical axi (the ordinate). Usually we plot the independent variable, which is the one more easily varied, on the abscissa, and the dependent variable on the ordinate. MATLAB has many functions and commands to produce various plots with special features. In this section we introduce the commands that are useful for making xy plots. In Section 5.8 we treat three-dimensional plots

The Anatomy of a Plot

The “anatomy” and nomenclature of a typical xy plot is shown in Figure 5.1-1, in which the plot of a data set and a curve generated from an equation appear. The scale on each axis refers to the range and spacing of the numbers. Both axes in this plot are said to be “rectilinear”–often shortened to linear-because the spacing of the numbers is regular; for example, the distance between the numbers 2 and 3 is the same as the distance between the numbers 4 and 5. Another type of scale is the logarithmic, which we explain later in this chapter. Tick marks are placed on the axis to help visualize the numbers being plotted. The tick-mark labels are the numbers that correspond to the tick-mark locations. (Some plots will have tick-mark labels that are not numbers; for example, if we plot temperature versus time of year, the tick-mark labels on the horizontal axis could be the names of months.) The spacing of the tick marks and their labels is important. We cover this topic later in the chapter. Each axis must have an axis label-also called an axis title. This label gives the name and units of the quantity plotted on that axis. An exception occurs when plotting a mathematical expression that has no physical interpretation; in that case the variables have no units. In addition, the plot often must have a plot title as well. The plot title is placed above the plot. A plot can be made from measured data or from an equation. When data is plotted, each-data point is plotted with a data symbol, or point marker; such as the

Nomenclature for a typical xy plot.

Nomenclature for a typical xy plot.

-small circle shown in Figure 5.1-1. A rare exception to this rule would be when there are so many data points that the symbols would be too densely packed. In that case, the data points should be plotted with a dot. However, when the plot is generated from a function, data symbols must never be used! Lines are always used to plot a function. Sometimes data symbols are connected by lines to help the viewer visualize the data, especially if there are few data points. However, connecting the data points-especially with a solid line-might imply knowledge of what occurs between the data points, and thus you should be careful to prevent such misinterpretation. When multiple curves or data sets are plotted, they must be distinguished from each other..One way of doing so is with a legend, which relates the data set LEGEND symbol or the curve’s line type to the quantity being plotted. Another method is to place a description (either text or an equation) near the curve or data symbols. We show examples of both methods later in the chapter

Requirements Cora Correct Plot

The following list describes the essential features of any plot:

  1. Each axis must be labeled with the name of the quantity being plotted and its units! If two or more quantities having different units are plotted (such as when plotting both speed and distance versus time), indicate the units in the axis.label if there is room, or in the legend or labels for each curve. Each axis should have regularly spaced tick marks at convenient intervals-no.t too sparse, but· not too dense-with a spacing that is easy to’ interpret and interpolate. For example, use 0.1, 0.2, and so.on, rather than 0.13, 0.26, and so on .
  2. If you are plotting more than one curve or data set, label each on its plot or use a legend to. distinguish them.
  3. If you are preparing multiple plots of a similar type or if the axes’ labels cannot convey enough information, use a title.
  4. If you are plotting measured data, plot each data point with a symbol such as a circle, square, or cross (use the same symbol very point in the same data set). If there are many data points, plot them using the dot symbol. Sometimes data symbols are connected by lines to. help the viewer visualize the data, especially if there are few data points. However, connecting the.data points, especially with a solid line, might be interpreted to. imply knowledge of what occurs between the data points. Thus you should be careful to. prevent such misinterpretation. . . If you are plotting points generated by’ evaluating a function (as o.p send to’ measured data), do not use a symbol to. plot the points. Instead, be sure to. generate many points, and connect the points with solid lines

Plot, Label, and Title Commands

The MATLAB basic xy plotting function is plot (x , y). If x and y are vectors, a single curve is plotted with the x values on the abscissa and the y values on the ordinate. The x label and y Label commands put labels on the abscissa and the ordinate, respectively. The syntax is x.label'( , text’ ), where text is the text of the label. Note that you must enelose the label’s text in single quotes. The syntax for y label is the same. The tit 1 e command puts a title at the top of’. the plot. Its syntax is t i.t Le ( , text’ ), where text is the title’s text. The following MATLAB session plots y.o.4.J1.8x for 0 :5 x :5 52, where y represents the height of a rocket after launch, in miles, and x is the horizontal (downrange) distance in miles. »x = [0:0.1:52); »y = 0.4*sqrt(1.8*x); »plot(x,y) »x label(‘Distance (miles)’)

The au,toscaling feature in MATLAB selects tick-mark spacing.

The auto s caling feature in MATLAB selects tick-mark spacing.

»y label(‘~eight (miles)’)

»title (‘Rocket Height as a Function of Downrange Distance’) I

Figure 5.1-2 .hows the plot. A spacing of 0.1 was selected for the x values to generate several hundred plotting points to produce a smooth curve. The plot (x , y) ~ in MATLAB automatically selects a tick-mark spacing for \ each axis and places -appropriate tick labels. This feature is called autoscating. MATLAB also chesean upper I~t f~r the x-axis, whidl is beyond the maximum value of 52·in the X” values. to obtain a convenient spacing. of 10 for the tick labels. A ticE-la~t spacing-of two would generate 27 labels. which gives a spacing so dense that the labels, would overlap one another. A spacing of 13 would work; but is not as convenient as a spcing of 10. Later you will learn to override the values selected’by MATLAB. . The axis label t tndpIt title are produced by the x label, y Label., and .  title commands. The 0rder’ Of the x label, y label. and tit le commands does not matter. but we must place them after the plot command. either on separa lines using ellipses or OR the same line separated by commas, as »x = ‘I00.1.:52) i »y = O.4~sqrt(1.8*x)i

»plot(x,y),x label (‘Distance (miles}’},y label(‘Height (miles}’}, …
title(‘Rocket Height· as a Function of DoIimrange Distance’)

The plot will appear in the Figure window. You can obtain a hard copy of the plot in one of several ways:

  1. Use the menu system. Select Print on the File menu in the Figure window. Answer OK when you are prompted to continue the printing process.
  2. Type print at the command line. This command sends the current plot directly to the printer.
  3. Save the plot to a file to be printed later or imported into another application such as a word processor. You need to know something about graphics file formats to use this file properly. See the subsection Exporting Figures later in this section.
  4. Type help print to obtain more information. MATLAB assigns the output of the plot command to figure window number 1. When another plot command is executed, MATLAB overwrites the contents of the existing figure window with the new plot. Although you can keep more than one figure window active, we do not use this feature in this text. When you have finished with the plot, close the figure window by selecting Close from the File menu in the figure window. Note that using the AIt- Tab key combination in Windows-based systems will,return you to the Command window without closing the figure window. If you do not close the window, it will not reappear when a new plot command is executed. However, the figure will still be updated.grid and axis Commands
    The grid command displays gridlines at the tick marks corresponding to the tick labels. Typ.grid on to add gridlines; type grid off to stop plotting gridlines. When used by itself, gr i d toggles this feature on or off, but you might want to use grid on and grid off to be sure. You can use the axi s command to override the MATLAB selections for the axis limits. The basic syntax is axis ( [xmin xmax ymin ymax]). This command sets the scaling for the x- and y-axes to the minimuin and maximum values indicated. Note that, unlike an array, this command does not use commas to separate the values. .
    The axi s command has the following variants:  axi s square, which selects the axes’ limits so that the plot will be square.
    • axis equal, which selects the scale factors and tick spacing to be the same on each axis. This variation makes plot (sin (x) , cas (x) ) look like a circle, instead of an oval.
    • axis auto, which returns the axis scaling to its default autoscaling mode in which the best axes limits are computed automatically. For example, to add a grid and to change the axes’ limits on the previous plot to 0 ::: x ::: 52 and 0 ::: y ::: 5, the session would look like »x [0: a . 1 : 52] ;»y = O.4*sqrt(1.8*x);

    The effects of the axis and grid commands.

    The effects of the axis and grid commands.

    .»plot(x,y) ,x label(‘Distance (miles) ‘),y label(‘Height (miles) ‘), …
    title(‘Rocket Height as a Function of Downrange Disance’), …
    grid on, axis([O 52 0 5])

    Figure 5.1-3 shows this plot. Notice how MATLAB chose a tick-label spacing of 5, not 13, for the x-axis. .
    This example illustrates how the printed plot can look different from the plot on the computer screen. MATLAB determines the number of tick-mark labels that can reasonably fit on the axis without being too densely spaced. A reasonable
    number for the computer screen is often different from the number for the printed output. In the preceding example, the screen plot showed labels on the x-axis at
    0, 10, 20, … , whereas the printed plot had labels at the intervals 0, 5, 10, 15, 20, …. You can eliminate this effect by using the tick-mark commands discussed later in the chapter.

    Plots of Complex Numbers
    With only one argument, say, plot (y) .the plot function will plot the values in the vectory versus their indices 1,2,3, … , and so on. If y is complex, plot (y) plots the imaginary parts versus the r ral parts.Thus plot (y) in this case is . equivalent to plot (real}. (y), image (y)) .This situation is the only time when the plot function handles L e imaginary parts; in all other variants of the plot

    Application oftbe plot (y) function. "

    Application oft be plot (y) function.

    function, it ignores the imaginary parts. For example, the script file
    z = 0.1 + O.9ii
    n = (0:0.01:10] i
    plot(z.~n),x label(‘Real’),y label(‘Imaginary’) . .
    generates the spiral shown in Figure 5.1-4. As you become more familiar with MATLAB, you will feel comfortable combining these commands as follows:
    plot ((0.1+0. ss: .” (0: 0.01: 10]) ,x label (‘Real’) ,y label (‘ Imaginary’)

    The Function Plot Command f plot
    MATLAB has a “smart” command for plotting functions, The f plot command automatically analyzes the function to be plotted and decides how many plotting points to use so that the.plot will show all the features of the function. Its syntax
    isf plot (“string’, l xmi n xmax]), where’ string’ is a text string
    that describes’ the function to be plotted and [xmin xmax] specifies the minimum and maximum values of the independent variable. The range of the dependent
    variable can also be specified. In this case the syntax is f plot ( , string’ , [xmin xmax ymin ymax] ) .

    A plot generated with the fplot command.

    A plot generated with the fplot command.

    For example. the session
    »f = os (tan(xll – tan(sin(xll’;
    »  f plot (f, [1 2) I
    produces the plot shown in Figure 5.1-5. You may combine the two demands into a single command as follows: f plot ( , cos (tan (x ) I –
    tan (sin (x) I ‘ , [1 2]). Always remember to enclose the function in single quotes. , Contrast this plot with the c;mesh own in Figure 5.1-6, which is produced by the plot command using 101 plotting points. »x = [1:0.01:2);
    »y = cos(tan(x)) – tan(sin(x));
    »plot(x,y l We can see that the f plot command automatically chose enough plotting points to display all the variations in the function. We can achieve the same plot using the plot command, but we need to know how many values to use in specifying the x vector. Another form is [x,y) = f plot(‘string’, limits), where limits may be eitber [xmin xmax] or Ixmi,n xmax ymin ymax]. With this form the command returns the abscissa and ordinate values in the column

    The function in Figure 5.1-5 generated with the plot command.

    The function in Figure 5.1-5 generated with the plot command.

    vectors x and y, but no plot is produced. The returned values can then be used for other purposes, such as plotting multiple curves, which is the topic of the next section. Other commands can be used with the f plot command to enhance a plot’s. appearance, for example, the title, x label, and y label commands and the line type commands to be introduced in the next section.

    Plotting Polynomials
    We can plot polynomials more easily by using the polyval function, introduced in Chapter 2. This function evaluates the polynomial at specified values of the independent variable. It requires only the polynomial’s coefficients and thus
    eliminates the need to type in the polynomial’s expression. For example, to plot the polynomial 3×5 +2×4 – l00x3 +2×2 – 7x +90 over the range -6 :::x :::6 with a spacing of 0.01, you type
    »x = [-6:0.01:6); »p = [3,2,-100,2,-7,90);
    »plot(x,polyval (p,x)),x label(‘x’),y label(‘p’)
    Table 5.1-1 summarizes the xy plotting commands introduced in this

    Basic xy plotting commands

    Basic xy plotting commands

    Test Your Understanding
    15.1-1 Redo the plot of the equation y = 0.4v’1] X shown in Figure 5.1-2 for  ::::x: ::::3:5 and 0 ::::y: ::::3: .5. –
    15.1-2 Use the f plot command to investigate the function.tan(cosx)-sin(tanx)
    for 0 ::::x: ::::2:rr. How many values of x are needed to obtain the same plot using the plot command? (Answer: 292 values.)
    15.1-3 Plot the imaginary part versus the real part of the function (0.2 +0.8;)” for 0 :::::n ::::: 20. Choose enough points to obtain a smooth curve. Label each axis and put a title on the plot. Use the axis command to change . the tick-label spacing.

    Saving Figures
    When you create a plot, the Figure window appears (see Figure 5.1-7). This window has eight menus, which are discussed in detail in Section 5.4. The File menu is used for saving and printing the figure. You can save your figure in a
    format that can be opened during another MATLAB session or in a format that can be used by other applications. .
    To save a figure that can be opened in subsequent MATLAB sessions, save it in a figure file with the .fig file name extension. To do this, select Save from the Figure window File menu or click the Save button (the disk icon) on the toolbar.
    If this is the first time you are saving the file, the Save As dialog box appears. Make sure that the type is MATLAB Figure (*.fig). Specify the name you want assigned to the figure file. Click OK. You can also use the save as command.

    An example of a Figure window.

    An example of a Figure window.

    To open a figure file, select Open from the File menu or click the Open button (the opened folder icon) on the toolbar. Select the figure file you want to open and click OK. The figure file appears in a new figure window. You can ‘also .
    use the open command. .
    Exporting Figures
    If you want to save the figure in a format that can be used by another application,such as the standard graphics file formats TIFF or EPS, perform these steps..

    1. Select Export Setup from the File menu. TID sdialog provides options you
    can specify for the output file, such as the figure size, fonts, line size and . style, and output format. ,..f(I.
    2. Select Export from the Export Setup dialog. A standard Save As dialog ppes.

    3. Select the format from the list of formats in the Save As type menu. This selects the format of the exported file and adds the standard file name extension given to files of that type.
    4. Enter the name you want to give the file, less the extension.
    S. Click Save You can also export the figure from the command line, by using the print command. See MATLAB help for more information about exporting figures in

    different formats On Windows systems, you can also copy a figure to the clipboard and then
    paste it into another application:
    1. Select Copy Options from the Edit menu. The Copying Options page of the Preferences dialog box appears.
    2. Complete the fields on the Copying Options page and click OK.
    3. Select Copy figure from the Edit menu.
    The figure is copied to the Windows clipboard and can be pasted into another application. MATLAB also enables, you to save figures in formats compatible with PowerPoint and MS Word. See the MATLAB help for more information.
    The.graphics functions covered in this section and in Sections 5.2 and 5.3 are sufficient to create detailed, professional-looking plots in MATLAB. These functions can be placed in script files that can reused to’ create similar plots.
    This feature gives them an advantage over the interactive plotting tools that are discussed in Section 5.4

Posted on July 29, 2015 in advanced Plotting and Model Building

Share the Story

Back to Top
Share This