2 Plots Same Graph Matlab
This example shows how to combine plots in the same axes using the hold
function, and how to create multiple axes in a figure using the tiledlayout
function. The tiledlayout
function is available starting in R2019b. If you are using an earlier release, use the subplot
function instead.
Combine Plots in Same Axes
Figure; subplot (1,2,1) plot (firstdata) subplot (1,2,2) plot (seconddata) This will create two axes areas within the same figure window. From your description, this is my best guess as to what you want. Edit: From the comments below, here is what you are doing. Add two more lines to the left side using the hold on command. Add an errorbar to the right side. The new plots use the same color as the corresponding y -axis and cycle through the line style order. The hold on command affects both the left and right sides. Combine Plots in Same Axes. By default, new plots clear existing plots and reset axes properties, such as the title. However, you can use the hold on command to combine multiple plots in the same axes. For example, plot two lines and a scatter plot. Then reset the hold state to off. X = linspace (0,10,50); y1 = sin (x); plot (x,y1) title ( 'Combine Plots' ) hold on y2 = sin (x/2); plot (x,y2) y3 = 2.sin (x); scatter (x,y3) hold. This will let you have two plots on the same figure so you can see them both at the same time and don't have to keep switching between figures. Subplot(1, 2, 1); plot(1:30). I mean: I have ten graphs to plot on the same figure. Each graph has its own (let's call it) related-graph, which has to be plotted on the same image. I want each graph and its related-graph to be the same color!
By default, new plots clear existing plots and reset axes properties, such as the title. However, you can use the hold on
command to combine multiple plots in the same axes. For example, plot two lines and a scatter plot. Then reset the hold state to off.
When the hold state is on, new plots do not clear existing plots or reset axes properties, such as the title or axis labels. The plots cycle through colors and line styles based on the ColorOrder
and LineStyleOrder
properties of the axes. The axes limits and tick values might adjust to accommodate new data.
Display Multiple Axes in a Figure
You can display multiple axes in a single figure by using the tiledlayout
function. This function creates a tiled chart layout containing an invisible grid of tiles over the entire figure. Each tile can contain an axes for displaying a plot. After creating a layout, call the nexttile
function to place an axes object into the layout. Then call a plotting function to plot into the axes. For example, create two plots in a 2-by-1 layout. Add a title to each plot.
Note: This code uses the tiledlayout
function, which is available starting in R2019b. If you are using an earlier release, use the subplot
function instead.
Create Plot Spanning Multiple Rows or Columns
To create a plot that spans multiple rows or columns, specify the span
argument when you call nexttile
. For example, create a 2-by-2 layout. Plot into the first two tiles. Then create a plot that spans one row and two columns.
Modify Axes Appearance
Modify the axes appearance by setting properties on each of the axes objects. You can get the axes object by calling the nexttile
function with an output argument. You also can specify the axes object as the first input argument to a graphics function to ensure that the function targets the correct axes.
For example, create two plots and assign the axes objects to the variables ax1
and ax2
. Change the axes font size and x-axis color for the first plot. Add grid lines to the second plot.
Control Spacing Around the Tiles
You can control the spacing around the tiles in a layout by specifying the Padding
and TileSpacing
properties. For example, display four plots in a 2-by-2 layout.
Minimize the spacing around the perimeter of the layout and around each tile by setting the Padding
and TileSpacing
properties to 'none'
.
Display Shared Title and Axis Labels
You can display a shared title and shared axis labels in a layout. Create a 2-by-1 layout t
. Then display a line plot and a stem plot. Synchronize the x-axis limits by calling the linkaxes
function.
Add a shared title and shared axis labels by passing t
to the title
, xlabel
, and ylabel
functions. Move the plots closer together by removing the x-axis tick labels from the top plot and setting the TileSpacing
property of t
to 'compact'
.
See Also
Functions
hold
nexttile
tiledlayout
title
Related Topics
This example shows how to create a chart with y-axes on the left and right sides using the yyaxis
function. It also shows how to label each axis, combine multiple plots, and clear the plots associated with one or both of the sides.
Plot Data Against Left y-Axis
Create axes with a y-axis on the left and right sides. The yyaxis left
command creates the axes and activates the left side. Subsequent graphics functions, such as plot
, target the active side. Plot data against the left y-axis.
Plot Data Against Right y-Axis
Activate the right side using yyaxis right
. Then plot a set of data against the right y-axis.
Add Title and Axis Labels
Control which side of the axes is active using the yyaxis left
and yyaxis right
commands. Then, add a title and axis labels.
Plot Additional Data Against Each Side
Add two more lines to the left side using the hold on
command. Add an errorbar to the right side. The new plots use the same color as the corresponding y-axis and cycle through the line style order. The hold on
command affects both the left and right sides.
Clear One Side of Axes
Clear the data from the right side of the axes by first making it active, and then using the cla
command.
Clear Axes and Remove Right y-Axis
Clear the entire axes and remove the right y-axis using cla reset
.
Now when you create a plot, it only has one y-axis. For example, plot three lines against the single y-axis.
Add Second y-Axis to Existing Chart
Matlab Two Plots Same Graph
Add a second y-axis to an existing chart using yyaxis
. The existing plots and the left y-axis do not change colors. The right y-axis uses the next color in the axes color order. New plots added to the axes use the same color as the corresponding y-axis.
See Also
Functions
2 Plots Same Graph Matlab Functions
cla
hold
plot
title
xlabel
ylabel
yyaxis