Main Content

Display Data with Multiple Scales and Axes Limits

You can use a variety of techniques to visualize data with multiple scales and axis limits. For example, you can use theyyaxisfunction to create plots with twoy-axes. To create plots with multiplex- andy-axes, multiple colorbars, or to create a plot with a discontinuous axis that is broken into intervals, use thetiledlayoutfunction.

Display Data with Twoy-Axes

Use theyyaxisfunction to create a plot with twoy-axes. For example, you can use twoy-axes to plot two lines on different scales.

创建一个n axes object, and activate the lefty-axis by callingyyaxis left. Then plot a sine wave.

figure yyaxisleftx = linspace(0,10); y = sin(3*x); plot(x,y)

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

Activate the righty-axis by callingyyaxis right. Then plot an amplified sine wave.

yyaxisrighty2 = sin(3*x).*exp(0.5*x); plot(x,y2)

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

Display Data with Multiplex-Axes andy-Axes

Since R2019b

To plot two sets of data with separatex- andy-axes, create two separate axes objects in a tiled chart layout. Within one of the axes objects, move thex-axis to the top of the plot box, and move they-axis to the right side of the plot box.

For example, you can create two plots that have differentx- andy-axis limits.

First, create two sets ofx- andy-coordinates.

x1 = 0:0.1:40; y1 = 4.*cos(x1)./(x1+2); x2 = 1:0.2:20; y2 = x2.^2./x2.^3;

创建一个tiled chart layout and an axes object. Then plot into the axes:

  • 创建一个1-by-1tiled chart layoutt.

  • 创建一个n axes objectax1by calling theaxesfunction and specifyingtas the parent object.

  • Plotx1andy1as a red line, and specifyax1as the target axes.

  • Change the color of thex-axis andy-axis to match the plotted line. Setting properties on the axes after plotting ensures that the settings persist.

t = tiledlayout(1,1); ax1 = axes(t); plot(ax1,x1,y1,'-r') ax1.XColor ='r'; ax1.YColor ='r';

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

创建一个second axes object and plot the second set of data in black rather than red. Then, set properties on the second axes object to move thex-axis andy-axis, and to ensure that neither plot obscures the other.

  • 创建一个n axes objectax2by calling theaxesfunction and specifyingtas the parent object.

  • Plotx2andy2as a black line, and specifyax2as the target axes.

  • Move thex-axis to the top, and move they-axis to the right.

  • Set the color of the axes object to'none'so that the underlying plot is visible.

  • Turn off the plot boxes to prevent the box edges from obscuring thex- andy-axes.

ax2 = axes(t); plot(ax2,x2,y2,'-k') ax2.XAxisLocation ='top'; ax2.YAxisLocation ='right'; ax2.Color ='none'; ax1.Box ='off'; ax2.Box ='off';

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

Plot Data on Discontinuousx-Axis

Since R2019b

You can use a tiled chart layout to give the appearance of a plot that is broken into intervals along one axis. For example, you might want to exclude one section of thex-axis to focus on other regions of interest.

Create coordinate vectorsxandy.

x = 0:0.1:60; y = 4.*cos(x)./(x+2);

创建一个tiled chart layout containing two tiles, and place an axes object across both tiles. In the final presentation, this axes object will appear in the background, behind two other axes objects. A section of itsx-axis will be visible to give the appearance of one longx-axis.

  • 创建一个1-by-2tiled chart layoutt, and specify compact tile spacing. Setting the tile spacing allows you to control the size of the gap between thex-axis intervals.

  • Create the background axesbgAxby calling theaxesfunction and specifyingtas the parent object. Specify name-value arguments to remove all the ticks and turn off the plot box.

  • Span the background axes across both tiles by setting theLayout.TileSpanproperty ofbgAxto[1 2].

figure t = tiledlayout(1,2,'TileSpacing','compact'); bgAx = axes(t,'XTick',[],'YTick',[],'Box','off'); bgAx.Layout.TileSpan = [1 2];

Figure contains an axes object. The axes object is empty.

创建一个n axes object in front ofbgAxin the first tile. Plotxandy, and set thex-axis limits to the first interval:

  • Createax1by calling theaxesfunction and specifyingtas the parent object. By default, the axes goes into the first tile.

  • Plot x and y intoax1.

  • Call thexlinefunction to display a dotted vertical line at the upper limit of the first interval.

  • Set thex-axis limits to the first interval,[0 15].

  • Add an axis label to identify the first interval.

ax1 = axes(t); plot(ax1,x,y) xline(ax1,15,':'); ax1.Box ='off'; xlim(ax1,[0 15]) xlabel(ax1,'First Interval')

Figure contains 2 axes objects. Axes object 1 is empty. Axes object 2 contains 2 objects of type line, constantline.

Repeat the process to create another axes object and plot for the second interval. The axes appears in the first tile by default. Move it to the second tile by setting theLayout.Tileproperty of the axes to2. Then, link the axes so that the limits of bothy-axes match.

% Create second plotax2 = axes(t); ax2.Layout.Tile = 2; plot(ax2,x,y) xline(ax2,45,':'); ax2.YAxis.Visible ='off'; ax2.Box ='off'; xlim(ax2,[45 60]) xlabel(ax2,'Second Interval')% Link the axeslinkaxes([ax1 ax2],'y')

Figure contains 3 axes objects. Axes object 1 is empty. Axes object 2 contains 2 objects of type line, constantline. Axes object 3 contains 2 objects of type line, constantline.

To add a title, pass the tiled chart layout to thetitlefunction.

title(t,'Attenuated Cosine Function')

Figure contains 3 axes objects. Axes object 1 is empty. Axes object 2 contains 2 objects of type line, constantline. Axes object 3 contains 2 objects of type line, constantline.

Display Two Sets of Data with Separate Colorbars

Since R2020b

一个坐标轴对象可以容纳only one colorbar. To create a visualization with multiple colorbars, stack multiple axes objects in a tiled chart layout. Make only one of the axes visible, but display a colorbar next to each of them in an outer tile of the layout.

Create the coordinate vectors, size data, and color data for two bubble charts.

x = 1:15; n = 70 * randn(1,15) + 50; y1 = n + x.^2; y2 = n - linspace(1,225,15); sz1 = rand(1,15); sz2 = rand(1,15); c = linspace(1,10,15);

Stack two axes objects, each containing a bubble chart, in a tiled chart layout.

  • 创建一个1-by-1tiled chart layoutt.

  • 创建一个xes objectax1and a bubble chart with thewintercolormap.

  • 创建一个xes objectax2and a bubble chart with theautumncolormap. Make this axes object invisible by setting theVisibleproperty to'off'.

  • Link the axes objects to keep them in sync. In this case, you can pass the children oftto thelinkaxesfunction. Alternatively, you can pass a vector of individual axes objects to the function.

% create first bubble chart with winter colormapt = tiledlayout(1,1); ax1 = axes(t); bubblechart(ax1,x,y1,sz1,c) colormap(ax1,'winter')秋天%创建第二个泡泡图colormapax2 = axes(t); bubblechart(ax2,x,y2,sz2,c) colormap(ax2,“秋天”) ax2.Visible ='off';% link the limits of axeslinkaxes(t.Children)

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

Display a colorbar with a label for each axes object in the east tile of the layout. The layout arranges the colorbars and keeps them aligned.

cb1 = colorbar(ax1); cb1.Layout.Tile ='east'; cb1.Label.String ='Time (s)'; cb2 = colorbar(ax2); cb2.Layout.Tile ='east'; cb2.Label.String ='Concentration (M)';

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

See Also

Functions

Related Topics