您在這裡

Multiple Plots in a Figure

20 一月, 2016 - 09:21

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:

    % This script generates sin(x) and cos(x) variables
clc                 %Clears command window
clear all           %Clears the variable space
close all           %Closes all figures
X1=[-2*pi:.1:2*pi];     %Creates a row vector from -2*pi to 2*pi with .1 increments
Y1=sin(X1);             %Calculates sine value for each x
Y2=cos(X1);             %Calculates cosine value for each x
Y3=Y1+Y2;               %Calculates sin(x)+cos(x)
Y4=Y1-Y2;               %Calculates sin(x)-cos(x)

 

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.

media/image6.png
Figure 3.5 Plot Tools 
 

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.

media/image7.png
Figure 3.6 Creating four sub plots. 
 

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).

media/image8.png
Figure 3.7 Adding data to axes. 
 

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

media/image9.png
Figure 3.8 Using "Property Editor". 
 

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

media/image10.png
Figure 3.9 The four subplots generated with "Plot Tools". 
 
media/image11.png
Figure 3.10 The four subplots in a single figure.