Display Data with Multiple Scales and Axes Limits - MATLAB & Simulink (2024)

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 the yyaxis function to create plots with two y-axes. To create plots with multiple x- and y-axes, multiple colorbars, or to create a plot with a discontinuous axis that is broken into intervals, use the tiledlayout function.

Display Data with Two y-Axes

Open Live Script

Use the yyaxis function to create a plot with two y-axes. For example, you can use two y-axes to plot two lines on different scales.

Create an axes object, and activate the left y-axis by calling yyaxis left. Then plot a sine wave.

figureyyaxis leftx = linspace(0,10);y = sin(3*x);plot(x,y)

Display Data with Multiple Scales and Axes Limits- MATLAB & Simulink (1)

Activate the right y-axis by calling yyaxis right. Then plot an amplified sine wave.

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

Display Data with Multiple Scales and Axes Limits- MATLAB & Simulink (2)

Display Data with Multiple x-Axes and y-Axes

Open Live Script

Since R2019b

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

For example, you can create two plots that have different x- and y-axis limits.

First, create two sets of x- and y-coordinates.

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

Create a tiled chart layout and an axes object. Then plot into the axes:

  • Create a 1-by-1 tiled chart layout t.

  • Create an axes object ax1 by calling the axes function and specifying t as the parent object.

  • Plot x1 and y1 as a red line, and specify ax1 as the target axes.

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

Display Data with Multiple Scales and Axes Limits- MATLAB & Simulink (3)

Create a 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 the x-axis and y-axis, and to ensure that neither plot obscures the other.

  • Create an axes object ax2 by calling the axes function and specifying t as the parent object.

  • Plot x2 and y2 as a black line, and specify ax2 as the target axes.

  • Move the x-axis to the top, and move the y-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 the x- and y-axes.

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

Display Data with Multiple Scales and Axes Limits- MATLAB & Simulink (4)

Plot Data on Discontinuous x-Axis

Open Live Script

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 the x-axis to focus on other regions of interest.

Create coordinate vectors x and y.

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

Create a 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 its x-axis will be visible to give the appearance of one long x-axis.

  • Create a 1-by-2 tiled chart layout t, and specify compact tile spacing. Setting the tile spacing allows you to control the size of the gap between the x-axis intervals.

  • Create the background axes bgAx by calling the axes function and specifying t as 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 the Layout.TileSpan property of bgAx to [1 2].

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

Display Data with Multiple Scales and Axes Limits- MATLAB & Simulink (5)

Create an axes object in front of bgAx in the first tile. Plot x and y, and set the x-axis limits to the first interval:

  • Create ax1 by calling the axes function and specifying t as the parent object. By default, the axes goes into the first tile.

  • Plot x and y into ax1.

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

  • Set the x-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')

Display Data with Multiple Scales and Axes Limits- MATLAB & Simulink (6)

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 the Layout.Tile property of the axes to 2. Then, link the axes so that the limits of both y-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')

Display Data with Multiple Scales and Axes Limits- MATLAB & Simulink (7)

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

title(t,'Attenuated Cosine Function')

Display Data with Multiple Scales and Axes Limits- MATLAB & Simulink (8)

Display Two Sets of Data with Separate Colorbars

Open Live Script

Since R2020b

An axes object can accommodate 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.

  • Create a 1-by-1 tiled chart layout t.

  • Create axes object ax1 and a bubble chart with the winter colormap.

  • Create axes object ax2 and a bubble chart with the autumn colormap. Make this axes object invisible by setting the Visible property to 'off'.

  • Link the axes objects to keep them in sync. In this case, you can pass the children of t to the linkaxes function. 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')% create second bubble chart with autumn colormapax2 = axes(t);bubblechart(ax2,x,y2,sz2,c)colormap(ax2,'autumn')ax2.Visible = 'off';% link the limits of axeslinkaxes(t.Children)

Display Data with Multiple Scales and Axes Limits- MATLAB & Simulink (9)

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)';

Display Data with Multiple Scales and Axes Limits- MATLAB & Simulink (10)

See Also

Functions

  • tiledlayout | yyaxis | axes

Related Topics

  • Combine Multiple Plots
  • Creating Colorbars

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.

Display Data with Multiple Scales and Axes Limits- MATLAB & Simulink (11)

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

Display Data with Multiple Scales and Axes Limits
- MATLAB & Simulink (2024)

FAQs

How do I limit axis values in MATLAB? ›

Change Axis Limits

Create a line plot. Specify the axis limits using the xlim and ylim functions. For 3-D plots, use the zlim function. Pass the functions a two-element vector of the form [min max] .

How to plot multiple axis in MATLAB? ›

Use the yyaxis function to create a plot with two y-axes. For example, you can use two y-axes to plot two lines on different scales. Create an axes object, and activate the left y-axis by calling yyaxis left . Then plot a sine wave.

How to plot two sets of data on one graph in MATLAB? ›

Best way I can suggest is to convert your data as cell arrays and use “plot” function to plot them. Documentation for “plot” function. You can use “hold on” and “hold off” to make sure the datasets are plotted on the same graph.

How to add axis labels in MATLAB? ›

You can add x-, y-, and z-axis labels using the xlabel , ylabel , and zlabel commands. For example, these statements label the axes and add a title. The labeling commands automatically position the text string appropriately. MATLAB interprets the characters immediately following the backslash "\" as TeX commands.

How do you display multiple plots in one figure in MATLAB? ›

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.

How to plot a graph with 2 variables in MATLAB? ›

A more complicated way

First, define the coordinate grid. Be sure to put the ; at the end of the command. Otherwise MATLAB will print the list of all of the x and y values for points in the grid. [X1,Y1] = meshgrid(-2*pi:0.2:2*pi,-2*pi:0.2:2*pi);

How do I plot multiple datasets on the same graph? ›

How to show two sets of data on one graph in Excel
  1. Enter data in the Excel spreadsheet you want on the graph. ...
  2. Select the data you want on the graph. ...
  3. Click the "Insert" tab and then look at the "Recommended Charts" in the charts group. ...
  4. Choose "All Charts" and click "Combo" as the chart type.
Apr 8, 2024

How do you show two sets of data on one graph? ›

On the All Charts tab, choose Combo, and then pick the Clustered Column - Line on Secondary Axis chart. Under Choose the chart type and axis for your data series , check the Secondary Axis box for each data series you want to plot on the secondary axis, and then change their chart type to Line.

How do I add axis labels in Simulink? ›

Add axis labels to the chart by using the xlabel and ylabel functions.

How do I customize axis in MATLAB? ›

You can change the x- or y-axis units by right-clicking the mouse on the axis label or by right-clicking on the plot and selecting Analysis Parameters.

How do I change the size of the axis values in MATLAB? ›

Here are a couple of different ways to change axis font size.
  1. x = linspace(-2*pi, 2*pi);
  2. y = cos(x);
  3. plot(x,y);
  4. set(gca,'FontSize',20, 'FontName', 'Courier')
  5. xlabel('x(t)') % Font Size and Name are taken from Line 4 command.
  6. ylabel('Y', 'FontSize', 15, 'FontName','Garamound', 'FontWeight','bold')
May 18, 2021

How do you hold on a specific axis in MATLAB? ›

hold( state ) sets the axes hold state by specifying a state value. You can specify state as "on" , "off" , a logical value, or an OnOffSwitchState value. hold( ax ,___) sets the hold state for the axes specified by ax instead of the current axes.

How do you set the Z-axis limit in MATLAB? ›

Specify Limits

zlim( limits ) sets the z-axis limits for the current axes. Specify limits as a two-element vector of the form [zmin zmax] , where zmax is greater than zmin . zlim( limitmethod ) specifies the limit method MATLAB® uses for automatic limit selection.

How do you write a limit function in MATLAB? ›

limit( f , a ) uses the default variable found by symvar . limit( f ) returns the limit at 0 . limit( f , var , a ,"left") returns the left-side limit of f as var approaches a . limit( f , var , a ,"right") returns the right-side limit of f as var approaches a .

Top Articles
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 5617

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.