Some Ways to Use Plotting Tools

Plotting Two Variables with Plotting Tools

Suppose you want to graph the function y = x3 over the x domain -1 to 1. The first step is to generate the data to plot.

It is simple to evaluate a function like this because the MATLAB® software can distribute arithmetic operations over all elements of a multivalued variable.

For example, the following statement creates a variable x that contains values ranging from -1 to 1 in increments of 0.1 (you could also use the linspace function to generate data for x). The second statement raises each value in x to the third power and stores these values in y:

x = -1:.1:1; % Define the range of x
y = x.^3;    % Raise each element in x to the third power

Now that you have generated some data, you can plot it using the MATLAB plotting tools. To start the plotting tools, type

plottools

A figure displays with plotting tools attached.

A simple line graph is a suitable way to display x as the independent variable and y as the dependent variable. To do this, select both variables (click to select, and then Shift+click to select again), and then right-click to display the context menu.

Select plot(x, y) from the menu. The line graph plots in the figure area. The black squares indicate that the line is selected and you can edit its properties with the Property Editor.

Changing the Appearance of Lines and Markers

Next, change the line properties so that the graph displays only the data point. Use the Property Editor to set following properties:

Adding More Data to the Graph

You can add more data to the graph by defining more variables or by specifying a MATLAB expression to generate data for the plot. This second approach makes it easy to explore variations of the data already plotted.

To add data to the graph, select the axes in the Plot Browser and click the Add Data button. When you are using the plotting tools, new data plots are always added to the existing graph, instead of replacing the graph, as it would if you issued repeated plotting commands. Thus, the plotting tools are in a hold on state.

To add data using the Plot Browser:

  1. Click the Edit Plot tool .

  2. Select the axes to which you wish to add data;
    handles appear around it.

  3. Click the Add Data button in the Plot Browser;
    the Add Data to Axes dialog box opens.

  4. Select a plot type from the Plot Type drop-down menu.

  5. Select a variable or type an expression for X Data Source.

  6. Select a variable or type an expression for Y Data Source.

  7. Click OK;
    a plot of the data you specified is added to the axes.

The preceding figure shows how to use the Add Data to Axes dialog box to create a line plot of y = x4, which is added to the existing plot of y = x3. The resulting plot appears with the Plot Browser, as shown in the following figure:

Changing the Type of Graph

The plotting tools enable you to easily view your data with a variety of plot types. The following picture shows the same data as above converted to stem plots. To change the plot type,

  1. Select both plotted series in the Plot Browser or Shift+click to select them in the plot itself.

  2. Select short dashes from the Line drop-down menu in the Property Inspector; the line type of both series changes.

  3. Select Stem from the Plot Type menu.

Modifying the Graph Data Source

You can link graph data to variables in your workspace. When you change the values contained in the variables, you can then update the graph to use the new data without having to create a new graph. (See also the refresh function.)

  1. Define 50 points between -3π and 3π and compute their sines and cosines:

    x = linspace(-3*pi,3*pi,50);
    ys = sin(x);
    yc = cos(x);
  2. Using the plotting tools, create a graph of ys = sin(x):

    figure
    plottools
  3. In the Figure Palette, alternate-click to select x and ys in the Variable pane.

  4. Right-click either selected variable and choose plot(x, ys) from the context menu, as the following figure shows.

You can use the Property Editor to change the data that this plot displays:

  1. Select the line ys vs x in the Plot Browser or by clicking it.

  2. In the Property Editor, select yc in the Y Data Source drop-down menu.

  3. Click the Refresh Data button;
    the plot will change to display a plot of yc vs x.

Providing New Values for the Data Source

Data values that define the graph are copies of variables in the base workspace (for example, x and y) to the XData and YData properties of the plot object (for example, a lineseries). Therefore, in addition to being able to choose new data sources, you can assign new values to workspace variables in the Command Window and click the Refresh Data button to update a graph to use the new data.

x = linspace(-pi,pi,50); % Define 50 points between -π and π
y = sin(x); 
area(x,y)		% Make an area plot of x and y

Now, recalculate y at the command line:

y = cos(x)

Select the blue line on the plot. Select, x as the X Data Source, y as the Y Data Source, and click Refresh Data. The graph's XData and YData are replaced, making the plot look like this.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS