 MATLAB - Lecture 22A Two Dimensional Plots / Chapter 5 Topics Covered:

Presentation on theme: "MATLAB - Lecture 22A Two Dimensional Plots / Chapter 5 Topics Covered:"— Presentation transcript:

MATLAB - Lecture 22A Two Dimensional Plots / Chapter 5 Topics Covered:
Engineering H192 Winter 2005 MATLAB - Lecture 22A Two Dimensional Plots / Chapter 5 Topics Covered: Plotting basic 2-D plots. The plot command. The fplot command. Plotting multiple graphs in the same plot. Formatting plots. Instructor notes: This presentation introduces 2D plotting in Matlab. It focuses on the plot and fplot commands for creating simple plots. Plotting is important because often times we can learn a lot more from a visual representation of data than we can from just looking at raw numbers. Instructional Objectives: Use plot and fplot commands in MATLAB to create 2-D plots Lecture 22A

Engineering H192 Winter 2005 105 MAKING X-Y PLOTS MATLAB has many functions and commands that can be used to create various types of plots. Instructor notes: We start with an example of some of the plots Matlab is capable of. x-y plot on the left, polar coordinate plot in the middle and 3D plot on the right with colored shading and mesh lines to better display the surface. In our class we will only create two dimensional x – y plots. Lecture 22A

106 EXAMPLE OF A 2-D PLOT Legend Plot title y axis label Text
Engineering H192 Winter 2005 106 EXAMPLE OF A 2-D PLOT Plot title Legend y axis label Text Tick-mark Data symbol Instructor notes: Here we see our first example of fairly full featured 2D plot. In the slides that follow we will see how to go about arriving at a plot like this. The plot has a title that describes the dependent and independent variables. It has a label for the x-axis and for the y-axis. Each of these axis labels indicate what is being plotted with the corresponding units included in parentheses. The plot has major tick marks on each axis and the scale is such that an easily read step size occurs between each tick mark. Because there is more than one data set being plotted, there is a legend that indicates which curve corresponds to which data set. Note that the theoretical data is just a line, while the experimental data is a collection of data points that are connected by line segments. Data symbols should be used for measured data only, not for theoretical curves. Finally, there is a small text box that describes what is being plotted. x axis label Tick-mark label Lecture 22A

106- 110 plot(x,y) TWO-DIMENSIONAL plot() COMMAND
Engineering H192 Winter 2005 106- 110 TWO-DIMENSIONAL plot() COMMAND The basic 2-D plot command is: plot(x,y) where x is a vector (one dimensional array), and y is a vector Both vectors must have the same number of elements. The plot command creates a single curve with the x values on the abscissa (horizontal axis) and the y values on the ordinate (vertical axis). The curve is made from segments of lines that connect the points that are defined by the x and y coordinates of the elements in the two vectors. Instructor notes: The starting point for making a basic 2D plot is the plot () command. This command generally takes two arguments, the first being an x vector and the second a y vector. The y vector will be plotted against the x vector. It is important to note that the two vectors must have the same number of elements. The plot that results will be a collection of line segments that connect the (x.y) pairs so that the more points there are to plot, the smoother the plot should look. This means that it may be helpful to have an idea of roughly what a plot should look like before it is drawn. Lecture 22A

106- 110 CREATING THE X AND Y VECTORS
Engineering H192 Winter 2005 106- 110 CREATING THE X AND Y VECTORS If data is given, the information is entered as the elements of the vectors x and y. If the values of y are determined by a function from the values of x, than a vector x is created first, and then the values of y are calculated for each value of x. The spacing (difference) between the elements of x must be such that the plotted curve will show the details of the function. Instructor notes: This slide should be self-explanatory. If you’re plotting data, then the data is entered as the elements of two vectors, one for the x-axis location and the other for the y-axis location. The vectors need to be of the same size so that each data point has a corresponding x-value and y-value. If the y values are mathematical functions of x, then an x vector is created first and a corresponding y vector is calculated by performing the function on each of the x values. When plotting measured data you have no control, once data is collected, over the resolution of your data. However, when plotting mathematical functions, you can exercise a lot of control. So, for functions it’s a good idea to make the increment size in the x vector small enough that the resulting plot shows the details of the function. Lecture 22A

106- 107 PLOT OF GIVEN DATA Given data:
Engineering H192 Winter 2005 106- 107 PLOT OF GIVEN DATA Given data: x y 1 2 3 5 7 7.5 8 6.5 5.5 4 6 10 A plot can be created by the commands shown below. This can be done in the Command Window, or by writing and then running a script file. >> x=[ ]; >> y=[ ]; >> plot(x,y) Instructor notes: Here is a quick example given two sets of data. First the x and y vectors are filled with the appropriate data and then the plot () command is executed. These actions could be performed in script or function files, as well as the command window. Again, note that there are the same number of elements in the x vector as there are in the y vector. Matlab complains loudly when the two don’t match. As soon as the plot () command executes, and additional window will be opened to display the data. Once the plot command is executed, the Figure Window opens with the following plot. Lecture 22A

106- 107 PLOT OF GIVEN DATA Engineering H192 Winter 2005
Instructor notes: Here we see a plot of the data given on the previous slide. Remember that the plot () command was issued simply as plot (x, y) so that the control over how the plot looks was left up to Matlab. Matlab picked the limits on the axes, it pick the step size increments and the color of the line that was plotted. The user can control all of these things, but in this simple example it was left up to Matlab. Control over these options will be discussed in subsequent slides. Lecture 22A

plot(x,y,’line specifiers’)
Engineering H192 Winter 2005 106- 107 LINE SPECIFIERS IN THE plot() COMMAND Line specifiers can be added in the plot command to: Specify the style of the line. Specify the color of the line. Specify the type of the markers (if markers are desired). plot(x,y,’line specifiers’) Instructor notes: One of the first ways to gain control over the appearance of a Matlab plot is through line specifiers. Line specifiers allow for control of the style of the line, such as solid, dashed, dotted, or dash-dot, the color of the line and the type of marker that will be placed at each (x,y) pair location. Markers are not required and typically not used for plots of mathematical functions. Line specifiers for a pair of vectors containing the x and y elements are supplied to the plot () command as a string in single quotes following the x and y vectors. Lecture 22A

plot(x,y,‘line specifiers’)
Engineering H192 Winter 2005 106- 107 LINE SPECIFIERS IN THE plot() COMMAND plot(x,y,‘line specifiers’) Line Specifier Line Specifier Marker Specifier Style Color Type Solid red r plus sign + dotted : green g circle o dashed blue b asterisk * dash-dot Cyan c point . magenta m square s yellow y diamond d black k Instructor notes: Here we see a table describing possible options for line specifiers. We can set the line style, the line color, and the marker type. The specifiers are passed to the plot () command as a string in single quotes. The order of the specifiers does not matter. Just because one line specifier is used, such as ‘—’ for dashed lines, does not mean that color and marker type line specifier must also be selected. So, any combination of 0-3 line specifiers can be used as long as no more than one from each category is used in for a single data set. There are additional marker types that are not included in this table. These can be accessed easily by entering help plot in the command window. Lecture 22A

107- 108 LINE SPECIFIERS IN THE plot() COMMAND
Engineering H192 Winter 2005 107- 108 LINE SPECIFIERS IN THE plot() COMMAND The specifiers are typed inside the plot() command as strings. Within the string the specifiers can be typed in any order. The specifiers are optional. This means that none, one, two, or all the three can be included in a command. EXAMPLES: plot(x,y) A solid blue line connects the points with no markers. plot(x,y,’r’) A solid red line connects the points with no markers. plot(x,y,’--y’) A yellow dashed line connects the points. plot(x,y,’*’) The points are marked with * (no line between the points.) plot(x,y,’g:d’) A green dotted line connects the points which are marked with diamond markers. Instructor notes: As has been mentioned, line specifiers are passed to the plot () command as strings in single quotes. The specifiers can appear within the single quotes in any order and are completely optional meaning that between 0 and 3 of them can be used, just don’t use more than one specifier from the same category for a single set of data. Some examples are listed here with explanations of what will appear Lecture 22A

PLOT OF GIVEN DATA USING LINE SPECIFIERS IN THE plot() COMMAND
Engineering H192 Winter 2005 110- 111 PLOT OF GIVEN DATA USING LINE SPECIFIERS IN THE plot() COMMAND Year Sales (M) 1988 1989 1990 1991 1992 1993 1994 127 130 136 145 158 178 211 >> year = [1988:1:1994]; >> sales = [127, 130, 136, 145, 158, 178, 211]; >> plot(year,sales,'--r*') Instructor notes: Here we see a quick example of plotting yearly sales. First the year vector is created. Next, the sales vector is created. Then, the sales vector is plotted on the y axis against the year vector on the x axis. We’re using a line specifier for this plot which tells the plot () command to create a plot with asterisks at each data point which are connected by a dashed line. The color of the line and the asterisks is red. It might be a good idea to point out as a reminder that because the year and sales lines end with a semi-colon, the output from these lines is suppressed. Remember this is particularly useful to use when the vector contains a lot of data points that would otherwise be echoed to the command window. It’s not such an inconvenience to forget the output suppression when there are just a few data points as in this example. Line Specifiers: dashed red line and asterisk markers. Lecture 22A

PLOT OF GIVEN DATA USING LINE SPECIFIERS IN THE plot() COMMAND
Engineering H192 Winter 2005 110- 111 PLOT OF GIVEN DATA USING LINE SPECIFIERS IN THE plot() COMMAND Instructor notes: Here we see the output from the commands issued on the previous slide. As prescribed by the line specifiers we get asterisks at each data point connected by a dashed line with both the line and asterisks in red. Again we have left the scaling of the axes and placement of tick marks completely up to Matlab. Also notice that this plot does not include a plot title nor labels on the axes. Dashed red line and asterisk markers. Lecture 22A

111- 112 CREATING A PLOT OF A FUNCTION Consider:
Engineering H192 Winter 2005 111- 112 CREATING A PLOT OF A FUNCTION Consider: A script file for plotting the function is: % A script file that creates a plot of % the function: 3.5^(-0.5x)*cos(6x) x = [-2:0.01:4]; y = 3.5.^(-0.5*x).*cos(6*x); plot(x,y) Creating a vector with spacing of 0.01. Calculating a value of y for each x. Instructor notes: Previous examples have shown how we would plot data. In this example we look at one way to plot a mathematical function. In this case we are given the function and the range over which we would like to plot the values. As the example indicates, we are plotting from within a script file in this case, but these commands could just as easily be entered directly into the command window. First we set up the x vector over our range of -2 to 4, inclusive. The increment size has been set to 0.01, which is fairly small and is going to generate vectors with a large number of elements. Consequently in this case it really is a good idea to end both the x vector line and the y vector line with semi-colons in order to suppress output to the command window. After the x vector is established, the y vector is determined by performing the function on each x vector element. This might also be a good point to remind students that element by element math is being performed here in the case of the exponentiation and the multiplication of the exponential term and the cosine term. Once the x and y vectors are determined, the plot () command is issued. Note that there are no line specifiers, so Matlab has complete control over the plot’s appearance. Once the plot command is executed, the Figure Window opens with the following plot. Lecture 22A

111- 112 A PLOT OF A FUNCTION Engineering H192 Winter 2005
Instructor notes: Here we see the output of the script file from the previous slide. Enough points were generated so that the plot accurately represents the output of the function. Note that there are no data point indicators and that the line is a solid blue line. Matlab always uses blue as the first default color. Again, the x and y axes limits have been determined by Matlab. Lecture 22A

111- 112 CREATING A PLOT OF A FUNCTION
Engineering H192 Winter 2005 111- 112 CREATING A PLOT OF A FUNCTION If the vector x is created with large spacing, the graph is not accurate. Below is the previous plot with spacing of 0.3. x = [-2:0.3:4]; y = 3.5.^(-0.5*x).*cos(6*x); plot(x,y) Instructor notes: Here we see a plot of the same function as from the previous two slides, but in this case the x vector was generated with a spacing of 0.3 rather than Obviously this plot does not represent the function nearly as well as the previous plot with more points. This is a good chance to point out to students that when making plots from mathematical functions, it is a good idea to experiment with the increments of the x vector points. They should play around with it a little and not automatically accept the first plot they’re able to generate. Lecture 22A

fplot(‘function’,limits)
Engineering H192 Winter 2005 112- 113 THE fplot COMMAND The fplot command can be used to plot a function with the form: y = f(x) fplot(‘function’,limits) The function is typed in as a string. The limits is a vector with the domain of x, and optionally with limits of the y axis: [xmin,xmax] or [xmin,xmax,ymin,ymax] Line specifiers can be added. Instructor notes: For plotting mathematical functions, in addition to the plot () command, we also have the fplot () command. This command is used specifically for plotting functions, hence the “f” at the beginning of the name. The function to be plotted is supplied to fplot () as a string in single quotes. The domain of the function is supplied as a vector that includes mininum and maximum x values. Additionally, limits can be placed on the minimum and maximum y values of the plot, too. It’s important to note that the function being passed to fplot () can contain only one variable because Matlab expects the variable that it sees in the function to be the independent variable. You cannot define a variable to have a specific value prior to the fplot () command and use that variable in the function. Matlab will not be able to distinguish which one represents the domain variable and which one has the constant value since Matlab will have to parse the string and form of the function and at the point of parsing the function is just a string. Lecture 22A

112- 113 PLOT OF A FUNCTION WITH THE fplot() COMMAND A plot of:
Engineering H192 Winter 2005 112- 113 PLOT OF A FUNCTION WITH THE fplot() COMMAND A plot of: >> fplot('x^2 + 4 * sin(2*x) - 1', [-3 3]) Instructor notes: Here we have a quick example of using the fplot () command. Note that the function is supplied to fplot () in single quotes and that there is only variable used in the function. As mentioned on the previous slide, it would not be valid to define another variable, for example p = 4, prior to the fplot () command and then use the p in place of the 4 that appears in the function. Note also that the vector [-3 3] specifies the domain over which values for the function will be calculated. If we had specified limits on the y values, for example by declaring a limits vector like [ ], the domain would have gone from -3 to 3, but the y values displayed would be cut off if they were below 0 or greater than 5. Lecture 22A

114- 116 PLOTTING MULTIPLE GRAPHS IN THE SAME PLOT
Engineering H192 Winter 2005 114- 116 PLOTTING MULTIPLE GRAPHS IN THE SAME PLOT Plotting two (or more) graphs in one plot: Using the plot command. 2. Using the hold on, hold off commands. Instructor notes: Matlab allows us a couple means of placing more than one graph on the same plot. We can do so by supplying multiple sets of arguments directly to the plot () command, or we can use the hold on/hold off commands in conjunction with issuing the plot () command multiple times. Lecture 22A

USING THE plot() COMMAND TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT
Engineering H192 Winter 2005 114- 115 USING THE plot() COMMAND TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT plot(x,y,u,v,t,h) Plots three graphs in the same plot: y versus x, v versus u, and h versus t. By default, MATLAB makes the curves in different colors. Additional curves can be added. The curves can have a specific style by adding specifiers after each pair, for example: Instructor notes: Here we demonstrate how to plot multiple graphs on the same plot. All that we need to do is supply multiple sets of vector pairs. Each vector in a pair must have the same number of elements, as we’ve seen before, but the different pairs do not need to have the same number of elements. For example, taken from this slide x and y must each have the same number of elements. And, u and v must have the same number as each other, but they are not required to have the same number as x and y. If we want to use line specifiers that override what Matlab would assign by default, the string that prescribes the line specifiers is supplied in single quotes immediately after the vector pair to which it corresponds. plot(x,y,’-b’,u,v,’—r’,t,h,’g:’) Lecture 22A

USING THE plot() COMMAND TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT
Engineering H192 Winter 2005 114- 115 USING THE plot() COMMAND TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT Plot of the function, and its first and second derivatives, for , all in the same plot. x = [-2:0.01:4]; y = 3*x.^3-26*x+6; yd = 9*x.^2-26; ydd = 18*x; plot(x,y,'-b',x,yd,'--r',x,ydd,':k') vector x with the domain of the function. Vector y with the function value at each x. Vector yd with values of the first derivative. Vector ydd with values of the second derivative. Instructor notes: Here we have an example of displaying multiple plots using a single plot () command as was just described on the previous slide. First the x vector is set up. Then the y, first derivative, and second derivative vectors are calculated. Finally, within a single plot () command the vector pairs followed by their corresponding line specifiers strings are supplied. Create three graphs, y vs. x (solid blue line), yd vs. x (dashed red line), and ydd vs. x (dotted black line) in the same figure. Lecture 22A

USING THE plot() COMMAND TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT
Engineering H192 Winter 2005 114- 115 USING THE plot() COMMAND TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT Instructor notes: This slide demonstrates the output of the plot () command from the previous slide. Again, Matlab sizes the axes and selects the increments on each axis. The line specifiers have given us a solid blue line for the function, a dashed red line for the first derivative, and a dotted black line for the second derivative. It might be worth pointing out that, again, there are no axis labels nor a graph title, but that we will be learning how to do this in a subsequent lecture. Lecture 22A

115- 116 USING THE hold on, hold off, COMMANDS
Engineering H192 Winter 2005 115- 116 USING THE hold on, hold off, COMMANDS TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT hold on Holds the current plot and all axis properties so that subsequent plot commands add to the existing plot. hold off Returns to the default mode whereby plot commands erase the previous plots and reset all axis properties before drawing new plots. Instructor notes: In addition to supplying multiple vector pairs to the plot () command, we can create graphs with multiple plots by using the hold on/hold off commands. Once a plot () command has been issued and hold on specified, all subsequent plot () commands will be plotted on the initial graph. Axis properties will be maintained for subsequent plots. If line specifiers are not specified in the subsequent plots, Matlab will use the default order and colors when drawing additional plots on the graph. To return to the default situation in which each plot () command causes a new plot to be drawn in the graph window, the hold off command must be issued. As the slide indicates, the hold on/hold off method is useful when not all information is available, such as when the Matlab is running in an interactive mode with the user and requesting information before continuing with plotting, or especially when the user is interacting with the plot directly. This method is useful when all the information (vectors) used for the plotting is not available a the same time. Lecture 22A

115- 116 USING THE hold on, hold off, COMMANDS
Engineering H192 Winter 2005 115- 116 USING THE hold on, hold off, COMMANDS TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT Plot of the function, and its first and second derivatives, for all in the same plot. x = [-2:0.01:4]; y = 3*x.^3-26*x+6; yd = 9*x.^2-26; ydd = 18*x; plot(x,y,'-b') hold on plot(x,yd,'--r') plot(x,ydd,':k') hold off First graph is created. Instructor notes: Here we see Matlab commands using the hold on/hold off method that will produce the same output as the commands listed on slide 20. Two more graphs are created. Lecture 22A

106 EXAMPLE OF A FORMATTED 2-D PLOT Plot title Legend y axis label
Engineering H192 Winter 2005 106 EXAMPLE OF A FORMATTED 2-D PLOT Plot title Legend y axis label Text Tick-mark Data symbol Instructor notes: This is an example we’ve seen before, but without quite so much attention to the detail of the plot. Students need to know that it is important to clearly and completely format or label a plot so that the person looking at it gets as much information out of the plot as quickly and easily as possible. To that end, plots should always be titled. The axes should be labeled indicating what kind of information is being displayed, for example “distance”, as well as the units on that information whenever possible. Each axis should contain tick marks which are labeled with endpoints and step sizes that make good use of available space and make it easy to decipher. Ideally, step sizes should be in twos or fives or tens. If more than one set of data appears on the plot, a legend should be used to indicate which data set is which. In some cases it may be desirable to add supplementary text to the plot to further explain the graphical data being provided. x axis label Tick-mark label Lecture 22A

Engineering H192 Winter 2005 116- 122 FORMATTING PLOTS A plot can be formatted to have a required appearance. With formatting you can: Add title to the plot. Add labels to axes. Change range of the axes. Add legend. Add text blocks. Add grid. Instructor notes: Matlab provides us with a host of formatting capabilities so that we can add all of the elements to a plot that make it easy for someone to use. We can format plots either through commands entered in a script/function file or directly from the command window. Lecture 22A

116- 122 FORMATTING PLOTS There are two methods to format a plot:
Engineering H192 Winter 2005 116- 122 FORMATTING PLOTS There are two methods to format a plot: Formatting commands. In this method commands, that make changes or additions to the plot, are entered after the plot() command. This can be done in the Command Window, or as part of a program in a script file. Formatting the plot interactively in the Figure Window. In this method the plot is formatted by clicking on the plot and using the menu to make changes or add details. Instructor notes: To reiterate...we can format plots via commands in the Command Window or script/function files. Additionally, plots can be formatted interactively using the icons provided in the Figure Window. In cases where multiple plots are being generated, it is probably most efficient to format them via commands such that Matlab takes care of the individual actions. Given a list of what to do, Matlab will be able to do it much more quickly. Lecture 22A

116- 122 FORMATTING COMMANDS title(‘string’)
Engineering H192 Winter 2005 116- 122 FORMATTING COMMANDS title(‘string’) Adds the string as a title at the top of the plot. xlabel(‘string’) Adds the string as a label to the x-axis. ylabel(‘string’) Adds the string as a label to the y-axis. axis([xmin xmax ymin ymax]) Sets the minimum and maximum limits of the x- and y-axes. Instructor notes: The title (), xlabel (), and ylabel () commands take a string argument in single quotes in order to label the top of the plot, the x-axis, and the y-axis, respectively. The axis () command takes a 2 or 4 element vector as its argument where the minimum and maximum x values are set or the minimum and maximum x and y values are set. Matlab will probably complain loudly if either xmax or ymax is less than xmin or ymin. The use of the axis () command allows the user to override the default setting that Matlab would otherwise pick. It can be use to look closely at a small window of data which would otherwise be unclear if a lot of points where available. It can be particularly useful when making an interactive plot that needs to resize itself. Lecture 22A

116- 122 FORMATTING COMMANDS legend(‘string1’,’string2’,’string3’)
Engineering H192 Winter 2005 116- 122 FORMATTING COMMANDS legend(‘string1’,’string2’,’string3’) Creates a legend using the strings to label various curves (when several curves are in one plot). The location of the legend is specified by the mouse. text(x,y,’string’) Places the string (text) on the plot at coordinate x,y relative to the plot axes. gtext(‘string’) Places the string (text) on the plot. When the command executes the figure window pops and the text location is clicked with the mouse. Instructor notes: The legend () command allows us to place a legend on a plot. The legend () command takes as its arguments a list of strings enclosed in single quotes that are the explanatory information you would like displayed next to a short sample of the line and data point display for a given set of data. The order of the strings must correspond to the order of the graphs that were added to the plot. In addition to the string, the legend () command can take a position argument that dictates where the legend is to be placed on the plot. The default is the upper right corner of the plot. The text () command takes three arguments: an x and y location to begin writing a string and the text string that you would like displayed. The string must be enclosed in single quotes. The x and y location must be supplied in the same scale that is being used for the x and y axes. In other words, the location of the text string is relative to the plot. Fixed screen locations do not exist. The gtext () command allows the user to interactively place text on the plot. When the gtext () command is executed the figure window becomes active and the next mouse click will place the text at the current mouse position. The text that is placed is the string that is supplied to the gtext () command. Lecture 22A

120- 121 EXAMPLE OF A FORMATTED PLOT
Engineering H192 Winter 2005 120- 121 EXAMPLE OF A FORMATTED PLOT Below is a script file of the formatted light intensity plot (2nd slide). (Some of the formatting options were not covered in the lectures, but are described in the book) x=[10:0.1:22]; y=95000./x.^2; xd=[10:2:22]; yd=[ ]; plot(x,y,'-','LineWidth',1.0) hold on plot(xd,yd,'ro--','linewidth',1.0,'markersize',10) hold off Creating vector x for plotting the theoretical curve. Creating vector y for plotting the theoretical curve. Creating a vector with coordinates of data points. Creating a vector with light intensity from data. Instructor notes: The next two slides show a brief script file example of formatting a plot. An x vector is created and a corresponding y vector that is the theoretical response to the x vector. In addition an x vector and a y vector are created for the measured data points. First the theoretical data is plotted. Note that the line width is specified in the plot. LineWidth has not been covered in class, but is available in the text. Next the hold on command is issued so that subsequent plot () operations appear on the same graph. The plot of data is then done, again with some formatting options that are covered in the text, but not mentioned in class. Then, the hold off command is issued. Lecture 22A

120- 121 EXAMPLE OF A FORMATTED PLOT
Engineering H192 Winter 2005 120- 121 EXAMPLE OF A FORMATTED PLOT Formatting of the light intensity plot (cont.) xlabel('DISTANCE (cm)') ylabel('INTENSITY (lux)') title('\fontname{Arial}Light Intensity as a Function of Distance','FontSize',14) axis([ ]) text(14,700,'Comparison between theory and experiment.','EdgeColor','r','LineWidth',2) legend('Theory','Experiment',0) Labels for the axes. Title for the plot. Setting limits of the axes. Instructor notes: This portion of the script begun on the previous slide is responsible for getting needed text on the graph as well as appropriately sizing the axes. The xlabel () and ylabel () commands label the x and y axes, respectively. It’s good to point out again to the students that not only are we saying what is being plotted, but also its units. The title () command puts a title at the top of the page. Note, again we see some formatting options that have no been mentioned in class, but are available in the text. The axis () command scales the x and y axes, overriding whatever defaults Matlab would otherwise use. The text () command places the given string on the plot beginning at (14, 700) relative to the axes being displayed in the plot. The EdgeColor and LineWidth options that are indicated cause the box around the text to be formatted with the specifications indicated. Again, not mentioned in class, but available in the text. The legend () command places the indicated text strings next to samples of the line and data points being used. Position 0 being supplied to the legend () command puts the legend inside the axes boundaries in a spot that least interferes with the graphs. Creating text. Creating a legend. The plot that is obtained is shown again in the next slide. Lecture 22A

120- 121 EXAMPLE OF A FORMATTED PLOT Engineering H192 Winter 2005
Instructor notes: Here we see the plot that results from the script file from the previous two slides. After having explained the previous two slides, it’s probably a good idea to quickly point out the plots features again. In fact, it might even be a good idea to move between this slide and the slide with the specific commands in order to make a visual connection between the words operations they cause. Lecture 22A

121- 122 FORMATTING A PLOT IN THE FIGURE WINDOW
Engineering H192 Winter 2005 121- 122 FORMATTING A PLOT IN THE FIGURE WINDOW Once a figure window is open, the figure can be formatted interactively. Use the insert menu to Use Figure, Axes, and Current Object-Properties in the Edit menu Click here to start the plot edit mode. Instructor notes: It’s faster to format plots via the commands, especially if scripts are being run repeatedly or many plots are needed, however, it is possible to format plots interactively through the figure window. Lecture 22A

Download ppt "MATLAB - Lecture 22A Two Dimensional Plots / Chapter 5 Topics Covered:"

Similar presentations