Main Content

tiledlayout

Create tiled chart layout

Since R2019b. Recommended oversubplot.

Description

example

tiledlayout(m,n)creates a tiled chart layout for displaying multiple plots in the current figure. The layout has a fixedm-by-ntile arrangement that can display up tom*nplots. If there is no figure, MATLAB®creates a figure and places the layout into it. If the current figure contains an existing axes or layout, MATLAB replaces it with a new layout.

A tiled chart layout contains an invisible grid of tiles that covers the entire figure or parent container. Each tile can contain an axes for displaying a plot. After creating a layout, call thenexttilefunction to place an axes object into the layout. Then call a plotting function to plot into the axes.

example

tiledlayout(arrangement)creates a layout that can hold any number of axes. Initially, there is only one empty tile that fills the entire layout. Specify an arrangement value to control the placement of subsequent axes:

  • "flow"— Create a layout for a grid of axes that can reflow depending on the size of the figure and the number of axes.

  • "vertical"— Create a layout for a vertical stack of axes.(since R2023a)

  • "horizontal"— Create a layout for a horizontal stack of axes.(since R2023a)

You can specify thearrangementargument without parentheses. For example,tiledlayout verticalcreates a layout for a vertical stack of axes.

example

tiledlayout(___,Name,Value)specifies additional options for the layout using one or more name-value pair arguments. Specify the options after all other input arguments. For example,tiledlayout(2,2,"TileSpacing","compact")creates a 2-by-2 layout with minimal spacing between the tiles. For a list of properties, seeTiledChartLayout Properties.

example

tiledlayout(,___)creates the layout in the specified parent container rather than in the current figure. Specify the parent container before all other input arguments.

example

t = tiledlayout(___)returns theTiledChartLayoutobject. Usetto configure properties of the layout after creating it.

Examples

collapse all

Create a2-by-2tiled chart layout, and call thepeaksfunction to get the coordinates of a predefined surface. Create an axes object in the first tile by calling thenexttilefunction. Then call thesurffunction to plot into the axes. Repeat the process using different plotting functions for the other three tiles.

tiledlayout(2,2); [X,Y,Z] = peaks(20);% Tile 1nexttile surf(X,Y,Z)% Tile 2nexttile contour(X,Y,Z)% Tile 3nexttile imagesc(Z)% Tile 4nexttile plot3(X,Y,Z)

Figure contains 4 axes objects. Axes object 1 contains an object of type surface. Axes object 2 contains an object of type contour. Axes object 3 contains an object of type image. Axes object 4 contains 20 objects of type line.

Create four coordinate vectors:x,y1,y2, andy3. Call thetiledlayoutfunction with the'flow'argument to create a tiled chart layout that can accommodate any number of axes. Call thenexttilefunction to create the first axes. Then ploty1in the first tile. This first plot fills the entire layout.

x = linspace (0, 30);日元= sin (x / 2);y2 = sin (x / 3);y3 = sin(x/4);% Plot into first tile three timestiledlayout('flow') nexttile plot(x,y1)

Figure contains an axes object. The axes object contains an object of type line.

Create a second tile and axes, and plot into the axes.

nexttile plot(x,y2)

Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line.

Repeat the process to create a third plot.

nexttile plot(x,y3)

Figure contains 3 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line. Axes object 3 contains an object of type line.

Repeat the process to create a fourth plot. This time, plot all three lines in the same axes by callinghold onafter plottingy1.

nexttile plot(x,y1) holdonplot(x,y2) plot(x,y3) holdoff

Figure contains 4 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line. Axes object 3 contains an object of type line. Axes object 4 contains 3 objects of type line.

Create a tiled chart layout that has a vertical stack of plots by specifying the"vertical"option when you call thetiledlayoutfunction. Then create three plots by calling thenexttilefunction followed by a plotting function. Each time you callnexttile, a new axes object is added to the bottom of the stack.

tiledlayout("vertical") x = 0:0.1:5; nexttile plot(x,sin(x)) nexttile plot(x,sin(x+1)) nexttile plot(x,sin(x+2))

Figure contains 3 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line. Axes object 3 contains an object of type line.

Create a tiled chart layout that has a horizontal stack of plots by specifying the"horizontal"option when you call thetiledlayoutfunction. Then create three plots by calling thenexttilefunction followed by a plotting function. Each time you callnexttile, a new axes object is added to the right side of the stack.

tiledlayout("horizontal") x = 0:0.1:10;nexttile情节(x, sin (x / 2)) nexttileplot(x,sin(x)) nexttile plot(x,sin(2*x))

Figure contains 3 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line. Axes object 3 contains an object of type line.

Create five coordinate vectors:x,y1,y2,y3, andy4. Then call thetiledlayoutfunction to create a2-by-2layout and specify a return argument to store theTileChartLayoutobject. Call thenexttilefunction to create an axes object in the next empty tile before calling theplotfunction.

x = linspace (0, 30);y1 = sin(x); y2 = sin(x/2); y3 = sin(x/3); y4 = sin(x/4); t = tiledlayout(2,2);% Tile 1nexttile plot(x,y1)% Tile 2nexttile plot(x,y2)% Tile 3nexttile plot(x,y3)% Tile 4nexttile plot(x,y4)

Figure contains 4 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line. Axes object 3 contains an object of type line. Axes object 4 contains an object of type line.

Decrease the amount of space between the tiles by setting theTileSpacingproperty to'compact'. Then decrease the space between the edges of the layout and the edges of the figure by setting thePaddingproperty to'compact'.

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

Figure contains 4 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type line. Axes object 3 contains an object of type line. Axes object 4 contains an object of type line.

Create a2-by-2tiled chart layoutt. Specify theTileSpacing名称-值对参数减少空间的选择ween the tiles. Then create a titled plot in each tile.

t = tiledlayout(2,2,'TileSpacing','Compact');% Tile 1nexttile plot(rand(1,20)) title('Sample 1')% Tile 2nexttile plot(rand(1,20)) title('Sample 2')% Tile 3nexttile plot(rand(1,20)) title('Sample 3')% Tile 4nexttile plot(rand(1,20)) title('Sample 4')

Figure contains 4 axes objects. Axes object 1 with title Sample 1 contains an object of type line. Axes object 2 with title Sample 2 contains an object of type line. Axes object 3 with title Sample 3 contains an object of type line. Axes object 4 with title Sample 4 contains an object of type line.

Display a shared title and axis labels by passingtto thetitle,xlabel, andylabelfunctions.

title(t,'Size vs. Distance') xlabel(t,'Distance (mm)') ylabel(t,'Size (mm)')

Figure contains 4 axes objects. Axes object 1 with title Sample 1 contains an object of type line. Axes object 2 with title Sample 2 contains an object of type line. Axes object 3 with title Sample 3 contains an object of type line. Axes object 4 with title Sample 4 contains an object of type line.

Create a panel in a figure. Then create a tiled chart layout in the panel by specifying the panel object as the first argument to thetiledlayoutfunction. Display a plot in each tile.

p = uipanel('Position',[.1 .2 .8 .6]); t = tiledlayout(p,2,1);% Tile 1nexttile(t) stem(1:13)% Tile 2nexttile(t) bar([10 22 31 43 52])

Figure contains 2 axes objects and another object of type uipanel. Axes object 1 contains an object of type stem. Axes object 2 contains an object of type bar.

Call thetiledlayoutfunction to create a2-by-1tiled chart layout. Call thenexttilefunction with an output argument to store the axes. Then plot into the axes, and set thex- and y-axis colors to red. Repeat the process in the second tile.

t = tiledlayout(2,1);% First tileax1 = nexttile; plot([1 2 3 4 5],[11 6 10 4 18]); ax1.XColor = [1 0 0]; ax1.YColor = [1 0 0];% Second tileax2 = nexttile; plot([1 2 3 4 5],[5 1 12 9 2],'o'); ax2.XColor = [1 0 0]; ax2.YColor = [1 0 0];

Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains a line object which displays its values using only markers.

Definescoresandstrikesas vectors containing bowling league data over four games. Then create a tiled chart layout and display three plots showing the number of strikes for each team.

scores = [444 460 380 387 366 500 365 451 611 548 412 452]; strikes = [9 6 5 6 4 8 4 7 16 10 9 8]; t = tiledlayout('flow');% Team 1nexttile plot([1 2 3 4],strikes(:,1),'-o') title('Team 1 Strikes')% Team 2nexttile plot([1 2 3 4],strikes(:,2),'-o') title('Team 2 Strikes')% Team 3nexttile plot([1 2 3 4],strikes(:,3),'-o') title('Team 3 Strikes')

Figure contains 3 axes objects. Axes object 1 with title Team 1 Strikes contains an object of type line. Axes object 2 with title Team 2 Strikes contains an object of type line. Axes object 3 with title Team 3 Strikes contains an object of type line.

Call thenexttilefunction to create an axes object that spans two rows by three columns. Then display a bar graph in the axes with a legend, and configure the axis tick values and labels. Call thetitle函数添加一个瓷砖the layout.

nexttile([2 3]); bar([1 2 3 4],scores) legend('Team 1',“团队2”,'Team 3','Location','northwest')% Configure ticks and axis labelsxticks([1 2 3 4]) xlabel('Game') ylabel('Score')% Add layout titletitle(t,'April Bowling League Data')

Figure contains 4 axes objects. Axes object 1 with title Team 1 Strikes contains an object of type line. Axes object 2 with title Team 2 Strikes contains an object of type line. Axes object 3 with title Team 3 Strikes contains an object of type line. Axes object 4 with xlabel Game, ylabel Score contains 3 objects of type bar. These objects represent Team 1, Team 2, Team 3.

To span an axes object from a specific location, specify the tile number and the span value.

Definescoresandstrikesas vectors containing bowling league data over four games. Then create a3-by-3tiled chart layout and display five bar graphs showing the number of strikes for each team.

scores = [444 460 380 388 389 387 366 500 467 460 365 451 611 426 495 548 412 452 471 402]; strikes = [9 6 5 7 5 6 4 8 10 7 4 7 16 9 9 10 9 8 8 9]; t = tiledlayout(3,3);% Team 1nexttile bar([1 2 3 4],strikes(:,1)) title('Team 1 Strikes')% Team 2nexttile bar([1 2 3 4],strikes(:,2)) title('Team 2 Strikes')% Team 3nexttile bar([1 2 3 4],strikes(:,3)) title('Team 3 Strikes')% Team 4nexttile bar([1 2 3 4],strikes(:,4)) title('Team 4 Strikes')% Team 5nexttile(7) bar([1 2 3 4],strikes(:,5)) title('Team 5 Strikes')

Figure contains 5 axes objects. Axes object 1 with title Team 1 Strikes contains an object of type bar. Axes object 2 with title Team 2 Strikes contains an object of type bar. Axes object 3 with title Team 3 Strikes contains an object of type bar. Axes object 4 with title Team 4 Strikes contains an object of type bar. Axes object 5 with title Team 5 Strikes contains an object of type bar.

Display a larger plot with a legend. Call thenexttilefunction to place the upper left corner of the axes in the fifth tile, and span the axes across two rows by two columns of tiles. Plot the scores for all the teams. Configure thex-axis to display four ticks, and add labels to each axis. Then add a shared title at the top of the layout.

nexttile(5,[2 2]); plot([1 2 3 4],scores,'-.') labels = {'Team 1',“团队2”,'Team 3','Team 4','Team 5'}; legend(labels,'Location','northwest')% Configure ticks and axis labelsxticks([1 2 3 4]) xlabel('Game') ylabel('Score')% Add layout titletitle(t,'April Bowling League Data')

Figure contains 6 axes objects. Axes object 1 with title Team 1 Strikes contains an object of type bar. Axes object 2 with title Team 2 Strikes contains an object of type bar. Axes object 3 with title Team 3 Strikes contains an object of type bar. Axes object 4 with title Team 4 Strikes contains an object of type bar. Axes object 5 with title Team 5 Strikes contains an object of type bar. Axes object 6 with xlabel Game, ylabel Score contains 5 objects of type line. These objects represent Team 1, Team 2, Team 3, Team 4, Team 5.

Create a 1-by-2 tiled chart layout. In the first tile, display a geographic plot containing a line that connects two cities on a map. In the second tile, create a scatter plot in polar coordinates.

tiledlayout(1,2)% Display geographic plotnexttile geoplot([47.62 61.20],[-122.33 -149.90],'g-*')% Display polar plotnexttile theta = pi/4:pi/4:2*pi; rho = [19 6 12 18 16 11 15 15]; polarscatter(theta,rho)

One of the ways that thenexttileoutput argument is useful is when you want to adjust the content in a previous tile. For example, you might decide to reconfigure the colormap used in a previous plot.

Create a 2-by-2 tiled chart layout. Call thepeaksfunction to get the coordinates for a predefined surface. Then create a different plot of the surface in each tile.

tiledlayout(2,2); [X,Y,Z] = peaks(20);% Tile 1nexttile surf(X,Y,Z)% Tile 2nexttile contour(X,Y,Z)% Tile 3nexttile imagesc(Z)% Tile 4nexttile plot3(X,Y,Z)

Figure contains 4 axes objects. Axes object 1 contains an object of type surface. Axes object 2 contains an object of type contour. Axes object 3 contains an object of type image. Axes object 4 contains 20 objects of type line.

To change the colormap in the third tile, get the axes in that tile. Call thenexttilefunction by specifying the tile number, and return the axes output argument. Then pass the axes to thecolormapfunction.

ax = nexttile(3); colormap(ax,cool)

Figure contains 4 axes objects. Axes object 1 contains an object of type surface. Axes object 2 contains an object of type contour. Axes object 3 contains an object of type image. Axes object 4 contains 20 objects of type line.

Create a 2-by-3 tiled chart layout containing two plots in individual tiles, and one plot that spans across two rows and two columns.

t = tiledlayout(2,3); [X,Y,Z] = peaks;% Tile 1nexttile contour(X,Y,Z)% Span across two rows and columnsnexttile([2 2]) contourf(X,Y,Z)% Last tilenexttile imagesc(Z)

Figure contains 3 axes objects. Axes object 1 contains an object of type contour. Axes object 2 contains an object of type contour. Axes object 3 contains an object of type image.

To change the colormap for the spanned axes, identify the tile location as one containing the upper-left corner of the axes. In this case, the upper-left corner is in the second tile. Call thenexttilefunction with2as tile location, and specify an output argument to return the axes object at that location. Then pass the axes to thecolormapfunction.

ax = nexttile(2); colormap(ax,hot)

Figure contains 3 axes objects. Axes object 1 contains an object of type contour. Axes object 2 contains an object of type contour. Axes object 3 contains an object of type image.

Load thepatientsdata set and create a table from a subset of the variables. Then create a2-by-2tiled chart layout. Display a scatter plot in the first tile, a heatmap in the second tile, and a stacked plot across the bottom two tiles.

loadpatientstbl = table(Diastolic,Smoker,Systolic,Height,Weight,SelfAssessedHealthStatus); tiledlayout(2,2)% Scatter plotnexttile scatter(tbl.Height,tbl.Weight)% Heatmapnexttile heatmap(tbl,'Smoker','SelfAssessedHealthStatus','Title','Smoker''s Health');% Stacked plotnexttile([1 2]) stackedplot(tbl,{'Systolic','Diastolic'});

Figure contains an axes object and other objects of type . The axes object contains an object of type scatter. The chart of type heatmap has title Smoker's Health.

Callnexttile, and specify the tile number as1to make the axes in that tile the current axes. Replace the contents of that tile with a scatter histogram.

nexttile(1) scatterhistogram(tbl,'Height','Weight');

Figure contains objects of type . The chart of type heatmap has title Smoker's Health.

When you want to share a colorbar or legend between two or more plots, you can place it in a separate tile.

Create filled contour plots of thepeaksandmembranedata sets in a tiled chart layout.

Z1 = peaks; Z2 = membrane; tiledlayout(2,1); nexttile contourf(Z1) nexttile contourf(Z2)

Figure contains 2 axes objects. Axes object 1 contains an object of type contour. Axes object 2 contains an object of type contour.

Add a colorbar, and move it to the east tile.

cb = colorbar; cb.Layout.Tile ='east';

Figure contains 2 axes objects. Axes object 1 contains an object of type contour. Axes object 2 contains an object of type contour.

Occasionally, you might need to create the axes by calling one of the axes functions (axes,polaraxes, orgeoaxes). When you create the axes with one of these functions, specify theargument as the tiled chart layout. Then position the axes by setting theLayout房地产在轴上。

Create a tiled chart layouttand specify the'flow'tile arrangement. Display a plot in each of the first three tiles.

t = tiledlayout('flow'); nexttile plot(rand(1,10)); nexttile plot(rand(1,10)); nexttile plot(rand(1,10));

Create a geographic axes objectgaxby calling thegeoaxesfunction and specifytas the parent argument. By default, the axes goes into the first tile, so move it to the fourth tile by settinggax.Layout.Tileto4. Span the axes across a2-by-3region of tiles by settinggax.Layout.TileSpanto[2 3].

gax = geoaxes(t); gax.Layout.Tile = 4; gax.Layout.TileSpan = [2 3];

Call thegeoplotfunction. Then configure the map center and zoom level for the axes.

geoplot(gax,[47.62 61.20],[-122.33 -149.90],'g-*') gax.MapCenter = [47.62 -122.33]; gax.ZoomLevel = 2;

Input Arguments

collapse all

Number of rows, specified as a positive whole number.

Example:tiledlayout(2,3)creates a tiled chart layout that has two rows and three columns of tiles.

Number of columns, specified as a positive whole number.

Example:tiledlayout(2,3)creates a tiled chart layout that has two rows and three columns of tiles.

Tile arrangement, specified as one of these values:

  • "flow"— Create a layout for a grid of axes. Each time you callnexttile, the layout reflows as needed to accommodate the new axes while maintaining an aspect ratio of roughly 4:3 for all the axes.

  • "vertical"— Create a layout for a vertical stack of axes. Each time you callnexttile, a new axes object is added to the bottom of the stack.(since R2023a)

  • "horizontal"— Create a layout for a horizontal stack of axes. Each time you callnexttile, a new axes object is added to the right side of the stack.(since R2023a)

Example:tiledlayout("vertical")creates a layout for a vertical stack of axes.

Parent container, specified as aFigure,Panel,Tab, orTiledChartLayoutobject.

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

Example:tiledlayout(2,2,"TileSpacing","compact")creates a 2-by-2 layout that has minimal spacing between the tiles.

Note

The properties listed here are only a subset. For a complete list, seeTiledChartLayout Properties.

Tile spacing, specified as"loose","compact","tight"or"none". Use this property to control the spacing between the tiles.

This table shows how each value affects the appearance of a2-by-2layout.

Value Appearance

"loose"

Tiled chart layout with

"compact"

Tiled chart layout with

"tight"

Tiled chart layout with

"none"

Tiled chart layout with

Padding around the perimeter of the layout, specified as"loose","compact", or"tight". The layout provides space for all decorations, such as axis labels, regardless of the value of this property.

This table shows how each value affects the appearance of a2-by-2layout.

Value Appearance

"loose"

Tiled chart layout with

"compact"

Tiled chart layout with

"tight"

Tiled chart layout with

中on History

Introduced in R2019b

expand all