Recap Plots with More than one Line Plots of Complex Arrays Line, Color and Mark Style Axis Scaling...

35
Lecture 24

Transcript of Recap Plots with More than one Line Plots of Complex Arrays Line, Color and Mark Style Axis Scaling...

Lecture 18

Lecture 24Recap Plots with More than one LinePlots of Complex ArraysLine, Color and Mark StyleAxis Scaling and Annotating PlotsSubplotsPolar PlotsLogarithmic PlotsBar Graphs and Pie ChartsHistogramsX-Y Graphs with two Y-axes

Function Plots

Three Dimensional Line PlotsThe plot3 function is similar to the plot function, except that it accepts data in three dimensionsInstead of just providing x and y vectors, the user must also provide a z vectorThese ordered triples are then plotted in three-space and connected with straight linesFor example:clear, clcx = linspace(0,10*pi,1000);y = cos(x);z = sin(x);plot3(x,y,z)gridxlabel('angle'), ylabel('cos(x)') zlabel('sin(x)') title('A Spring')The title, labels, and grid are added to the graph in the usual way, with the addition of zlabel for the z -axis.The coordinate system used with plot3 is oriented using the right-handed coordinate system familiar to engineers

Surface Plots Surface plots allow to represent data as a surfaceTwo types of surface plotsMesh plots Surf plotsMesh Plots There are several ways to use mesh plotsThey can be used to good effect with a single two-dimensional mxn matrixIn this application, the value in the matrix represents the z- value in the plotThe x- and y- values are based on the matrix dimensions Take, for example, the following very simple matrix:z = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10;2, 4, 6, 8, 10, 12, 14, 16, 18, 20;3, 4, 5, 6, 7, 8, 9, 10, 11, 12];The codemesh(z)xlabel('x-axis')ylabel('y-axis')zlabel('z-axis') generates the graph

Explanation of Graph The graph is a mesh created by connecting the points defined in z into a rectilinear gridNotice that the x -axis goes from 0 to 10 and y goes from 1 to 3The matrix index numbers were used for the axis valuesMesh Plots Continued.The mesh function can also be used with three arguments: mesh(x,y,z) In this case, x is a list of x -coordinates, y is a list of y -coordinates, and z is a list of z -coordinates.x = linspace(1,50,10)y = linspace(500,1000,3)z = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10;2, 4, 6, 8, 10, 12, 14, 16, 18, 20;3, 4, 5, 6, 7, 8, 9, 10, 11, 12]The x vector must have the same number of elements as the number of columns in the z vector and the y vector must have the same number of elements as the number of rows in the z vectorThe commandmesh(x,y,z)creates the plot Notice that the x -axis varies from 0 to 50, with data plotted from 1 to 50

Surf Plots Surf plots are similar to mesh plots, but surf creates a three-dimensional colored surface instead of a meshThe colors vary with the value of z The surf command takes the same input as mesh : either a single inputFor example: surf(z) , in which case it uses the row and column indices as x and y -coordinatesor three matricesThe commandsurf(x,y,z) using the same data as for mesh creates the graph

Continued.The shading scheme for surface plots is controlled with the shading commandThe default (shown in previous slide) is faceted. Interpolated shading can create interesting effectsThe plot(in next slide) is created by adding shading interpto the previous list of commands

Continued.Flat shading without the grid is generated whenshading flatis used

Continued.The color scheme used in surface plots can be controlled with the colormap functionFor example:colormap(gray)forces a grayscale representation for surface plotsThis may be appropriate if youll be making black-and-white copies of your plotsOther available colormaps areautumn bone hotspring colorcube hsvsummer cool pinkwinter copper prismjet (default) flag whiteExampleA more complicated surface can be created by calculating the values of Z :x= [-2:0.2:2];y= [-2:0.2:2];[X,Y] = meshgrid(x,y);Z = X.*exp(-X.^2 - Y.^2);In the preceding code, the meshgrid function is used to create the two-dimensional matrices X and Y from the one-dimensional vectors x and y The values in Z are then calculated

The following code plots the calculated values:subplot(2,2,1)mesh(X,Y,Z)title('Mesh Plot'), xlabel('x-axis'), ylabel('y-axis'),zlabel('z-axis')subplot(2,2,2)surf(X,Y,Z)title('Surface Plot'), xlabel('x-axis'), ylabel('y-axis'),zlabel('z-axis')Either the x , y vectors or the X , Y matrices can be used to defi ne the x and y -axes.

Contour Plots Contour plots are two-dimensional representations of three-dimensional surfaces, much like the familiar contour maps used by many hikersThe contour command was used to create Figure c, and the surfc command was used to create Figure d(in next slide) :subplot(2,2,3)contour(X,Y,Z)xlabel('x-axis'), ylabel('y-axis'), title('Contour Plot')subplot(2,2,4)surfc(X,Y,Z)xlabel('x-axis'), ylabel('y-axis')title('Combination Surface and Contour Plot')

Pseudo Color PlotsPseudo color plots are similar to contour plots, except that instead of lines outlining a specific contour, a two-dimensional shaded map is generated over a gridMATLAB includes a sample function called peaks that generates the x , y , and z matrices of an interesting surface that looks like a mountain range:[x,y,z] = peaks;With the following code, we can use this surface to demonstrate the use of pseudo color plotssubplot(2,2,1)pcolor(x,y,z)The grid is deleted when interpolated shading is used:subplot(2,2,2)pcolor(x,y,z)shading interpContinued.You can add contours to the image by overlaying a contour plot:subplot(2,2,3)pcolor(x,y,z)shading interphold oncontour(x,y,z,20,'k')The number 20 specifies that 20 contour lines are drawn, and the 'k' indicates that the lines should be blackIf we hadnt specified black lines, they would have been the same color as the pseudo color plot and would have disappeared into the imageFinally, a simple contour plot was added to the figure for comparison:subplot(2,2,4)contour(x,y,z)

Editing Plots From The Menu BarIn addition to controlling the way plots look by using MATLAB commands, plots can be edited after thses have createdThe plot in next slide was created with the sphere command, which is one of several sample functions, like peaks , used to demonstrate plottingsphereContinued.In the figure, the Insert menu has been selectedNotice that you can insert labels, titles, legends, text boxes, and so on, all by using this menuThe Tools menu allows you to change the way the plot looks, by zooming in or out, changing the aspect ratio, etc.The figure toolbar, underneath the menu toolbar, offers icons that allow you to do the same thing

Continued.The plot in the previous slide doesnt really look like a sphere; its also missing labels and a title, and the meaning of the colors may not be clearThis plot can be edited by first adjusting the shape:Select Edit -> Axes Properties from the menu toolbarFrom the Property Editor>Axes window, select More Properties -> Data Aspect Ratio Mode Set the mode to manual

Continued.Similarly, labels, a title, and a color bar were added (figure shown in next slide) using the Property EditorThey could also have been added by using the Insert menu option on the menu barEditing plot in this manner is more interactive and allows to fine-tune the plots appearanceThe only problem with editing a figure interactively is that if MATLAB program is run again, and all improvements will be lost

Creating Plots from Workspace Window A great feature of MATLAB is its ability to create plots interactively from the workspace windowIn this window, select a variable, then select the drop-down menu on the plotting icon (shown in Figure in next slide )MATLAB will list the plotting options it thinks are reasonable for the data stored in variableSimply select the appropriate option, and your plot is created in the current figure window If you dont like any of the suggested types of plot, choose More plots from the drop-down menu, and a new window will open with the complete list of available plotting options for you to choose fromThis is especially useful, because it may suggest options that had not occurred For example: (figure in next slide) scatter plot of the x and y matrices highlighted in the figure. The matrices were created by loading the seamount data set, which is built into MATLAB

Continued.If you want to plot more than one variable, highlight the first, then hold down the Ctrl key and select the additional variablesTo annotate plots, use the interactive editing processThe interactive environment is a rich resourceThe more information is get out by exploring and experimenting