When working with data visualizations in Matlab, it is often necessary to plot multiple graphs in a single figure. This can be achieved by creating multiple axes within the figure. Each axes represents a separate plot area where you can create different types of graphs or combine different plots together.
To plot another graph in axes, you first need to create the axes using the axes function. This function takes in the position of the axes within the figure, specified as a vector of four values representing the normalized coordinates of the lower-left corner and the width and height of the axes. Once you have created the axes, you can use any plotting function, such as plot or scatter, to create the desired graph within that axes.
For example, if you have already created a figure and an axes using the figure and axes functions, you can create another graph in a new axes by calling the plot function with the new axes as the first argument. This will plot the graph within the specified axes, allowing you to have multiple graphs in the same figure.
In addition to creating multiple graphs in a figure, you can also customize each axes individually. This includes setting the axis limits, labels, titles, colors, and other properties specific to each axes. By utilizing multiple axes, you have the flexibility to create complex and informative data visualizations in Matlab.
How to Create Multiple Graphs in MATLAB: A Step-by-Step Guide
When working with data in MATLAB, it is often necessary to visualize multiple graphs in a single figure. This allows for easy comparison and analysis of different datasets. Fortunately, MATLAB provides a simple way to create multiple graphs within the same axes.
Step 1: Create the Figure and Axes
To begin, you need to create a new figure and axes. This can be done using the figure() and axes() functions. The figure() function creates a new figure window, while the axes() function creates a new set of axes within the figure.
Example:
figure;
ax = axes;
This will create a new figure window and assign the axes to the variable ax.
Step 2: Plot the First Graph
Next, you can plot the first graph within the axes. This can be done using the standard plot() function in MATLAB. Specify the data points for the x and y coordinates as input arguments to the plot() function.
Example:
x = 1:10;
y = sin(x);
plot(ax, x, y);
This will plot a sine wave on the axes ax.
Step 3: Plot Additional Graphs
To plot additional graphs, use the same process as step 2. Call the plot() function and pass the axes variable as the first input argument.
Example:
Here, two graphs – sine wave and cosine wave – are plotted on the same axes.
Step 4: Customize the Graphs
After plotting the multiple graphs, you can customize them by adding labels, titles, legends, and other visual elements. MATLAB provides numerous functions to adjust various aspects of the graphs, such as xlabel(), ylabel(), title(), legend(), etc.
Example:
This will add labels, a title, and a legend to the graph.
By following these simple steps, you can easily create multiple graphs within a single axes in MATLAB. This allows for convenient analysis and comparison of different datasets, helping you gain valuable insights from your data.
Step 1: Create a New Figure
The first step in plotting another graph in axes is to create a new figure in MATLAB. A new figure provides a new window in which the graph can be displayed. To create a new figure, you can use the figure
function in MATLAB. Here is an example:
figure;
This code creates a new figure with default settings. You can also specify additional properties for the figure, such as the size and position, by using name-value pairs as arguments in the figure
function. For example:
figure('Position', [100 100 800 600]);
This code creates a new figure with a size of 800 pixels by 600 pixels, and it is positioned at (100, 100) on the screen.
Once you have created a new figure, you can plot the graph in the new figure by using the plot
function or any other plotting function in MATLAB. The graph will be displayed in the new figure window.
Creating a new figure is useful when you want to plot multiple graphs side by side or compare graphs. Each figure can have its own set of axes and plots.
Step 2: Define Axes for the First Graph
In order to plot another graph in the same figure, you need to define axes for the first graph. Axes provide a coordinate system that allows for positioning and scaling of the graph elements.
To define axes, you can use the subplot
function in MATLAB. This function is commonly used to create multiple subplots in a single figure, but it can also be used to define the axes for a single graph.
First, you need to specify the number of rows, number of columns, and the position of the current axes. For example, if you want to create a single graph in the entire figure, you can use the following code:
subplot(1,1,1)
This code creates a subplot with 1 row, 1 column, and sets the current axis position to the first subplot.
Once you have defined the axes, you can use the standard MATLAB plotting functions, such as plot
or scatter
, to plot the desired data on the graph. You can also customize the axes properties, such as the axis limits, labels, and titles.
By defining axes for the first graph, you have created a canvas on which you can plot multiple graphs in the same figure. In the next step, we will discuss how to plot another graph in the same axes.
Step 3: Plot the First Graph
Once you have created your axes, you can start plotting your data on the graph. To plot the first graph, follow these steps:
- Create the data points for the first graph. These data points should be stored in a matrix or vector in MATLAB.
- Use the
plot
function to plot the data points on the graph. The syntax for theplot
function isplot(x, y)
, wherex
andy
are the vectors representing the x and y coordinates of the data points. - Customize the appearance of the graph by adding titles, labels, and legends. You can use functions like
title
,xlabel
,ylabel
, andlegend
to add these elements to the graph. - Adjust the limits of the axes if necessary. You can use the
xlim
andylim
functions to set the limits of the x and y axes, respectively. - Finally, display the graph by calling the
show
function.
By following these steps, you will be able to plot your first graph on the axes in MATLAB.
Step 4: Define Axes for the Second Graph
After creating the first graph in the axes, now we will define the axes for the second graph. This can be done by creating a new set of axes with specific position and size within the same figure.
To define the axes for the second graph, follow these steps:
Step 1:
Create a new set of axes by using the axes
function.
Example:
ax2 = axes;
Step 2:
Specify the position and size of the new axes by setting the Position
property.
Example:
ax2.Position = [0.15 0.15 0.35 0.35];
In the above example, the position and size of the new axes are specified as [0.15 0.15 0.35 0.35]. This means that the new axes will start at 15% of the figure width and 15% of the figure height, with a width and height of 35% of the figure width and height, respectively.
Step 3:
Plot the desired data on the new axes using the appropriate plotting function.
Example:
plot(ax2, xData2, yData2);
Replace xData2
and yData2
with your actual data arrays.
By following these steps, you can define the axes for the second graph in MATLAB. This allows you to plot multiple graphs within the same figure, making it easier to analyze and compare different data sets.
Step 5: Plot the Second Graph
To plot the second graph in the same axes, we can use the plot
function again with the new data. Since we already have the axes object axes1
created in the previous steps, we can simply call the plot
function on axes1
to plot the second graph.
Here’s the code snippet:
plot(axes1, x2, y2)
This code will plot the second graph with the data points stored in the variables x2
and y2
. Make sure to replace x2
and y2
with your own data points.
Once the code is executed, you should see the second graph overlaid on the first graph in the same axes. You can customize the appearance of the second graph using the various plotting options available in MATLAB.
Now you have successfully plotted the second graph in the same axes. Continue to the next steps if you want to further customize the graph or add any additional data.