# Graphics

## Graphing with MATLAB


       
  

A picture is worth a thousand words, particularly visual representation of data in engineering is very useful. MATLAB has powerful graphics tools and there is a very helpful section devoted to graphics in MATLAB Help: Graphics. Students are encouraged to study that section; what follows is a brief summary of the main plotting features. 

### Two-Dimensional Plots


### The plot Statement

Probably the most common method for creating a plot is by issuing plot(x, y) statement where function y is plotted against x.
Having variables assigned in the Workspace, x and y=sin(x) in our case, we can also select x and y, and right click on the selected variables. This opens a menu from which we choose plot(x,y). See the figure below.


### Annotating Plots

Graphs without labels are incomplete and labeling elements such as plot title, labels for x and y axes, and legend should be included. Using up arrow, recall the statement above and add the annotation commands as shown below.

Run the file and compare your result with the first one.



### Superimposed Plots

If you want to merge data from two graphs, rather than create a new graph from scratch, you can superimpose the two using a simple trick:




### Multiple Plots in a Figure

Multiple plots in a single figure can be generated with subplot in the Command Window. However, this time we will use the built-in Plot Tools. Before we initialize that tool set, let us create the necessary variables using the following script: 
Note that the above script clears the command window and variable workspace. It also closes any open Figures. After running the script, we will have X1, Y1, Y2, Y3 and Y4 loaded in the workspace. Next, select File > New > Figure, a new Figure window will open. Click "Show Plot Tools and Dock Figure" on the tool bar.


Under New Subplots > 2D Axes, select four vertical boxes that will create four subplots in one figure. Also notice, the five variables we created earlier are listed under Variables.


After the subplots have been created, select the first supblot and click on "Add Data". In the dialog box, set X Data Source to X1 and Y Data Source to Y1. Repeat this step for the remaining subplots paying attention to Y Data Source (Y2, Y3 and Y4 need to be selected in the subsequent steps while X1 is always the X Data Source).



Next, select the first item in "Plot Browser" and activate the "Property Editor". Fill out the fields as shown in the figure below. Repeat this step for all subplots.


Save the figure as sinxcosx.fig in the current directory.




### Three-Dimensional Plots

3D plots can be generated from the Command Window as well as by GUI alternatives. This time, we will go back to the Command Window.


### The plot3 Statement

With the X1,Y1,Y2 and Y2 variables still in the workspace, type in plot3(X1,Y1,Y2) at the MATLAB prompt. A figure will be generated, click "Show Plot Tools and Dock Figure".



Use the property editor to make the following changes.

The final result should look like this:

Use help or doc commands to learn more about 3D plots, for example, image(x), surf(x) and mesh(x).

### Quiver or Velocity Plots

To plot vectors, it is useful to draw arrows so that the direction of the arrow points the direction of the vector and the length of the arrow is vector’s magnitude. However the standard plot function is not suitable for this purpose. Fortunately, MATLAB has quiver function appropriately named to plot arrows. 
quiver(x,y,u,v) plots vectors as arrows at the coordinates (x,y) with components (u,v). The matrices x, y, u, and v must all be the same size and contain corresponding position and velocity components.



### Summary of Key Points

1. plot(x, y) and plot3(X1,Y1,Y2) statements create 2- and 3-D graphs respectively,
2. Plots at minimum should contain the following elements: title, xlabel, ylabel and legend,
3. Annotated plots can be easily generated with GUI Plot Tools,
4. quiver and quiver3 plots are useful for making vector diagrams.