1 / 70

CMPS 1371 Introduction to Computing for Engineers

Learn how engineers use the xy plot for visualization. Create multiple plots, change styles, and add annotations with MATLAB. Explore different types of 2-D plots for enhanced data representation.

marioshaw
Download Presentation

CMPS 1371 Introduction to Computing for Engineers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PLOTTING CMPS 1371Introduction to Computing for Engineers

  2. Plotting

  3. Two Dimensional Plots The xy plot is the most commonly used plot by engineers The independent variable is usually called x The dependent variable is usually called y

  4. Consider xy data Time is the independent variable and distance is the dependent variable

  5. Plot function Define x and y and call the plot function You can use any variable name that is convenient for the dependent and independent variables

  6. Engineers always add … • Title • X axis label, complete with units • Y axis label, complete with units • Often it is useful to add a grid

  7. Multiple plots MATLAB overwrites the figure window every time you request a new plot To open a new figure window use the figure function – for example figure(2) Create plot on same graph hold on Freezes the current plot, so that an additional plot can be overlaid When you use this approach the additional line is drawn in blue – the default drawing color

  8. The first plot is drawn in blue

  9. The hold on command freezes the plot The second line is also drawn in blue, on top of the original plot To unfreeze the plot use the hold off command

  10. Same graph You can also create multiple lines on a single graph with one command Using this approach each line defaults to a different color

  11. Each set of ordered pairs will produce a new line

  12. Variations • If you use the plot command with a single matrix, MATLAB plots the values versus the index number

  13. Variations • If you want to create multiple plots, all with the same x value you can… • Use alternating sets of ordered pairs • plot(x,y1,x,y2,x,y3,x,y4) • Or group the y values into a matrix • z=[y1,y2,y3,y4] • plot(x,z)

  14. Matrix of Y values Alternating sets of ordered pairs

  15. Peaks The peaks(100) function creates a 100x100 array of values. Since this is a plot of a single variable, we get 100 different line plots

  16. Line, Color and Mark Style • You can change the appearance of your plots by selecting user defined • line styles • color • mark styles • Try using: • help plot for a list of available styles

  17. Available choices

  18. Specify your choices in a string • plot(x,y,‘linestyle markstyle color') • strings are identified with a tick (apostrophe) mark • if you don’t specify style, a default is used • line style – none • mark style – none • color – blue • Consider: plot(x,y,’:ok’) • the : means use a dotted line • the o means use a circle to mark each point • the letter k indicates that the graph should be drawn in black • (b indicates blue)

  19. dotted line circles black

  20. specify the drawing parameters for each line after the ordered pairs that define the line

  21. Axis scaling • MATLAB automatically scales each plot to completely fill the graph • If you want to specify a different axis – use the axis command axis([xmin,xmax,ymin,ymax]) • Lets change the axes on the graph we just looked at

  22. Use the axis function to override the automatic scaling

  23. Annotating Your Plots • You can also add • legends • textbox • Of course you should always add • title • axis labels

  24. Improving your labels You can use Greek letters in your labels by putting a backslash (\) before the name of the letter. For example: title(‘\alpha \beta \gamma’) creates the plot title αβγ To create a superscript use curly brackets title(‘x^{2}’) gives x2

  25. Subplots • The subplot command allows you to subdivide the graphing window into a grid of m rows and n columns • subplot(m,n,p) rows columns location

  26. subplot(2,2,1) 2 columns 1 2 2 rows 4 3

  27. 2 rows and 1 column

  28. Other Types of 2-D Plots • Polar Plots • Logarithmic Plots • Bar Graphs • Pie Charts • Histograms • X-Y graphs with 2 y axes • Function Plots

  29. Polar Plots • Some functions are easier to specify using polar coordinates than by using rectangular coordinates • Use polar(x,y) • For example the equation of a circle is • y=sin(x) in polar coordinates

  30. Logarithmic Plots • A logarithmic scale (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. • data varies exponentially

  31. Plots • plot(x,y) • – uses a linear scale on both axes • semilogy(x,y) • uses a log10 scale on the y axis • semilogx(x,y) • uses a log10 scale on the x axis • loglog(x,y) • use a log10 scale on both axes

  32. Bar Graphs and Pie Charts • MATLAB includes a whole family of bar graphs and pie charts • bar(x) – vertical bar graph • barh(x) – horizontal bar graph • bar3(x) – 3-D vertical bar graph • bar3h(x) – 3-D horizontal bar graph • pie(x) – pie chart • pie3(x) – 3-D pie chart

  33. Bar Graphs

  34. Histograms • A histogram is a plot showing the distribution of a set of values Defaults to class size of 10

  35. X-Y Graphs with Two Y Axes • Sometimes it is useful to overlay two x-y plots onto the same figure. • However, if the order of magnitude of the y values are quite different, it may be difficult to see how the data behave.

  36. Scaling Depends on the largest value plotted • Example Its difficult to see how the blue line behaves, because the scale isn’t appropriate

  37. plotyy function for 2 y-axis The plotyy function allows you to use two scales on a single graph

  38. Function Plots • Function plots allow you to use a function as input to a plot command, instead of a set of ordered pairs of x-y values • fplot('sin(x)',[-2*pi,2*pi]) function input as a string range of the independent variable – in this case x

  39. Three Dimensional Plotting • Line plots • Surface plots • Contour plots

  40. The z-axis is labeled the same way the x and y axes are labeled Three Dimensional Line Plots • These plots require a set of order triples ( x-y-z values) as input • Use plot3(x,y,z)

  41. MATLAB uses a coordinate system consistent with the right hand rule

  42. Just for fun • try the comet3 function, which draws the graph in an animation sequence • comet3(x,y,z) • If your animation draws too slowly, add more data points • For 2-D line graphs use the comet function

  43. Surface Plots • Represent x-y-z data as a surface • mesh - meshplot • surf – surface plot • Can use both mesh and surface plots to good effect with a single two dimensional matrix

  44. The x and y coordinates are the matrix index numbers

More Related