Combine Multiple Plots - MATLAB & Simulink (2024)

Since R2019b. Replaces Combine Multiple Plots (R2019a).

Open Live Script

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.

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 ony2 = sin(x/2);plot(x,y2)y3 = 2*sin(x);scatter(x,y3) hold off

Combine Multiple Plots- MATLAB & Simulink (1)

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.

x = linspace(0,10,50);y1 = sin(x);y2 = rand(50,1);tiledlayout(2,1)% Top plotnexttileplot(x,y1)title('Plot 1')% Bottom plotnexttilescatter(x,y2)title('Plot 2')

Combine Multiple Plots- MATLAB & Simulink (2)

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.

x = linspace(0,10,50);y1 = sin(x);y2 = rand(50,1);% Top two plotstiledlayout(2,2)nexttileplot(x,y1)nexttilescatter(x,y2)% Plot that spansnexttile([1 2])y2 = rand(50,1);plot(x,y2)

Combine Multiple Plots- MATLAB & Simulink (3)

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.

x = linspace(0,10,50);y1 = sin(x);y2 = rand(50,1);tiledlayout(2,1)% Top plotax1 = nexttile;plot(ax1,x,y1)title(ax1,'Plot 1')ax1.FontSize = 14;ax1.XColor = 'red';% Bottom plotax2 = nexttile;scatter(ax2,x,y2)title(ax2,'Plot 2')grid(ax2,'on')

Combine Multiple Plots- MATLAB & Simulink (4)

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.

x = linspace(0,30);y1 = sin(x);y2 = sin(x/2);y3 = sin(x/3);y4 = sin(x/4);% Create plotst = tiledlayout(2,2);nexttileplot(x,y1)nexttileplot(x,y2)nexttileplot(x,y3)nexttileplot(x,y4)

Reduce the spacing around the perimeter of the layout and around each tile by setting the Padding and TileSpacing properties to 'compact'.

t.Padding = 'compact';t.TileSpacing = 'compact';

Combine Multiple Plots- MATLAB & Simulink (6)

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.

x1 = linspace(0,20,100);y1 = sin(x1);x2 = 3:17;y2 = rand(1,15);% Create plots.t = tiledlayout(2,1);ax1 = nexttile;plot(ax1,x1,y1)ax2 = nexttile;stem(ax2,x2,y2)% Link the axeslinkaxes([ax1,ax2],'x');

Combine Multiple Plots- MATLAB & Simulink (7)

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

% Add shared title and axis labelstitle(t,'My Title')xlabel(t,'x-values')ylabel(t,'y-values')% Move plots closer togetherxticklabels(ax1,{})t.TileSpacing = 'compact';

Combine Multiple Plots- MATLAB & Simulink (8)

See Also

Functions

  • tiledlayout | nexttile | title | hold

Related Topics

  • Create Chart with Two y-Axes
  • Specify Axis Tick Values and Labels

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Combine Multiple Plots- MATLAB & Simulink (9)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Combine Multiple Plots
- MATLAB & Simulink (2024)

FAQs

How to combine multiple plots into one matlab? ›

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.

How to merge two scopes in Simulink? ›

Direct link to this answer
  1. Assuming you mean two scopes, there are two ways:
  2. Edit a scope block to accept two inputs ('Number of axes' setting in Scope Parameters - General) and move both signal into it. ...
  3. Move both signals into a Mux Block and the output of the Mux block into a scope.
Mar 11, 2014

How to plot multiple lines in Simulink? ›

Plot Multiple Lines

Use the figure command to open a new figure window. You can plot multiple lines using the hold on command. Until you use hold off or close the window, all plots appear in the current figure window.

How do I combine multiple plots into one? ›

In R Programming Language, you can combine plots using 'par' function. Combining plots will help you to make decisions easily. Comparing results will be easy by combining plots. par() function is used to set the parameters for multiple plots, and the layout() function determines how the plots should be arranged.

How to combine two models in MATLAB? ›

You can merge the changes between the two Simulink models by clicking the Merge Mode button in the toolstrip. This creates a third file, targetFile , which can contain the changes from either the left model ( sl_aircraft1) or right model ( sl_aircraft2 ).

What is merge in Simulink? ›

The Merge block combines inputs into a single output. The output value at any time is equal to the most recently computed output of its driving blocks. Specify the number of inputs by setting the Number of inputs parameter.

How to combine two signals in Simulink? ›

To combine signals that update at the same time into an array or matrix signal, use a Vector Concatenate block. The left side of the model contains two blocks Repeating Sequence blocks from the Simulink Sources library. Each block generates a scalar output signal of the form displayed on the block icon.

How to make subplots in Simulink? ›

Use the Simulink. sdi. setSubPlotLayout function to configure a 2 -by- 1 subplot layout in the Simulation Data Inspector plotting area. Then, use the plotOnSubplot function to plot the sine signal on the top subplot and the cosine signal on the lower subplot.

How to plot data from Simulink in MATLAB? ›

From the Simulink Editor, right-click the logging badge for a signal, and select Properties to open the Instrumentation Properties for the signal. Then, click the graphic representation of the Line. From the Instrumentation Properties, you can also select subplots where you want to plot the signal.

How to plot multiple bar plots in MATLAB? ›

To plot a single series of bars, specify y as a vector of length m. The bars are positioned from 1 to m along the x-axis. To plot multiple series of bars, specify y as a matrix with one column for each series.

How do you add a second plot in MATLAB? ›

Use hold on to add a second line plot without deleting the existing line plot. The new plot uses the next color and line style based on the ColorOrder and LineStyleOrder properties of the axes.

How to plot two subplots in MATLAB? ›

Create a figure with two subplots. Assign the Axes objects to the variables ax1 and ax2 . Specify the Axes objects as inputs to the plotting functions to ensure that the functions plot into a specific subplot. Modify the axes by setting properties of the Axes objects.

What is the difference between plot and subplot in MATLAB? ›

The difference is that the subplot deals with issues in a storyline that aren't essential to the plot, whereas the plot deals with major events in the storyline that contribute to the arc of it.

How do you combine sets of data in MATLAB? ›

dsnew = combine( ds1,ds2,...,dsN ) combines multiple datastores to create a CombinedDatastore object using the data returned by the read function on the input datastores. dsnew = combine( ds1,ds2,...,dsN ,ReadOrder= order ) combines multiple datastores based on the specified read order.

How to combine multiple MATLAB files into one? ›

We can now efficiently merge the contents of the two .mat files together:
  1. >> copyfile('A.mat','C.mat') % C will be the merged file.
  2. >> S = load('B.mat');
  3. >> save('C.mat','-struct','S','-append')
Sep 18, 2018

How to plot two images together in MATLAB? ›

C = imfuse( A , B ) creates a composite image from two images, A and B . If A and B are different sizes, imfuse pads the smaller dimensions with zeros so that both images are the same size before creating the composite. The output, C , is a numeric matrix containing a fused version of images A and B .

How do you combine multiple arrays in MATLAB? ›

You can use the square bracket operator [] to concatenate or append arrays. For example, [A,B] and [A B] concatenates arrays A and B horizontally, and [A; B] concatenates them vertically.

Top Articles
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 6464

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.