4/8/2022

2 Plots Matlab

25

Hi I have 3 scripts which all produce a figure. They are all graphs. How would i put them all on one script and have all plots displayed as separate figures. MATLAB Graphics 2-D and 3-D Plots. Tags plot graphs; Community Treasure Hunt. Find the treasures in MATLAB Central and discover how the community can help you!

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 2 Plots Matlab

Matlab

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.

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.

Matlab plot two series

Create Plot Spanning Multiple Rows or Columns

Plots

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

Legends are a useful way to label data series plotted on a graph. These examples show how to create a legend and make some common modifications, such as changing the location, setting the font size, and adding a title. You also can create a legend with multiple columns or create a legend for a subset of the plotted data.

Create Simple Legend

Create a figure with a line chart and a scatter chart. Add a legend with a description for each chart. Specify the legend labels as inputs to the legend function.

Specify Labels Using DisplayName

Alternatively, you can specify the legend labels using the DisplayName property. Set the DisplayName property as a name-value pair when calling the plotting functions. Then, call the legend command to create the legend.

Legends automatically update when you add or delete a data series. If you add more data to the axes, use the DisplayName property to specify the labels. If you do not set the DisplayName property, then the legend uses a label of the form 'dataN'.

2 Plots Matlab

Add a scatter chart for 2017 data.

Customize Legend Appearance

The legend function creates a Legend object. Legend objects have properties that you can use to customize the appearance of the legend, such as the Location, Orientation, FontSize, and Title properties. For a full list, see Legend Properties.

You can set properties in two ways:

  • Use name-value pairs in the legend command. In most cases, when you use name-value pairs, you must specify the labels in a cell array, such as legend({'label1','label2'},'FontSize',14).

  • Use the Legend object. You can return the Legend object as an output argument from the legend function, such as lgd = legend. Then, use lgd with dot notation to set properties, such as lgd.FontSize = 14.

Legend Location and Orientation

Specify the legend location and orientation by setting the Location and Orientation properties as name-value pairs. Set the location to one of the eight cardinal or intercardinal directions, in this case, 'northwest'. Set the orientation to 'vertical' (the default) or 'horizontal', as in this case. Specify the labels in a cell array.

Legend Font Size and Title

Specify the legend font size and title by setting the FontSize and Title properties. Assign the Legend object to the variable lgd. Then, use lgd to change the properties using dot notation.

Legend with Multiple Columns

Create a chart with six line plots. Add a legend with two columns by setting the NumColumns property to 2.

Include Subset of Charts in Legend

Combine two bar charts and a scatter chart. Create a legend that includes only the bar charts by specifying the Bar objects, b1 and b2, as the first input argument to the legend function. Specify the objects in a vector.

See Also

Matlab

Multiple Plots Matlab Color

legend Legend Properties