Create chart with two y-axes (2024)

Create chart with two y-axes

collapse all in page

Syntax

yyaxis left

yyaxis right

yyaxis(ax,___)

Description

example

yyaxis left activates the side of the current axes associated with the left y-axis. Subsequent graphics commands target the left side. If the current axes do not include two y-axes, then this command adds a second y-axis. If there are no axes, then this command first creates them.

yyaxis right activates the side of the current axes associated with the right y-axis. Subsequent graphics commands target the right side.

example

yyaxis(ax,___) specifiesthe active side for the axes ax instead of thecurrent axes. If the axes do not include two y-axes,then this command adds a second y-axis. Specifythe axes as the first input argument. Use single quotes around 'left' and 'right'.

Examples

collapse all

Plot Data Using Two y-Axes

Open Live Script

Create axes with a y-axis on both the left and right sides. Plot a set of data against the left y-axis. Then, use yyaxis right to activate the right side so that subsequent graphics functions target it. Plot a second set of data against the right y-axis and set the limits for the right y-axis.

x = linspace(0,10);y = sin(3*x);yyaxis leftplot(x,y)z = sin(3*x).*exp(0.5*x);yyaxis rightplot(x,z)ylim([-150 150])

Create chart with two y-axes (1)

Add Title and Axis Labels to Each Side

Create a chart with two y-axes and add a title and axis labels to each side.

Load the matrix hwydata from the example file accidents.mat. Create a scatter plot of the fifth column in hwydata against the left y-axis. Add a title and axis labels.

load('accidents.mat','hwydata')ind = 1:51;drivers = hwydata(:,5);yyaxis leftscatter(ind,drivers)title('Highway Data')xlabel('States')ylabel('Licensed Drivers (thousands)')

Create chart with two y-axes (2)

Create a second scatter plot of the seventh column in hwydata against the right y-axis. Then, label the right y-axis.

pop = hwydata(:,7);yyaxis rightscatter(ind,pop)ylabel('Vehicle Miles Traveled (millions)')

Create chart with two y-axes (3)

Plot Multiple Sets of Data on Each Side

Open Script

Plot two lines against the left y-axis by using the hold on command.

x = linspace(0,10);yl1 = sin(x);yl2 = sin(x/2);yyaxis leftplot(x,yl1)hold onplot(x,yl2)

Create chart with two y-axes (4)

Plot two lines against the right y-axis. The hold command affects both the left and right y-axes, so you do not need to reissue it. After plotting, turn hold back off.

yr1 = x;yr2 = x.^2;yyaxis rightplot(x,yr1)plot(x,yr2)hold off

Create chart with two y-axes (5)

Clear the left side by making it active and then using the cla command.

yyaxis leftcla

Create chart with two y-axes (6)

Control Colors for Each Side

Open Live Script

Specify the color scheme for each side of the axes by setting the color order to the two colors that you want to use. Starting in R2019b, you can use the colororder function to set the color order. Then, plot two lines against the left y-axis and two lines against the right y-axis. Add a legend.

colororder({'b','m'})yyaxis lefty = [1 2; 3 4];plot(y)yyaxis rightz = [4 3; 2 1];plot(z)legend

Create chart with two y-axes (7)

Control Individual Plot Colors

Open Live Script

Control individual plot colors by setting the color order for each side of the axes.

Plot three bar charts against the left side. Use a different color for each bar series by setting the color order for the left side to the default color order.

yyaxis leftbar(magic(3));colororder('default')

Plot three scatter plots against the right side. Use a different color for each scatter plot by setting the color order to an array of color names. Alternatively, you can specify the colors using a matrix of RBG triplets. Then add a legend.

yyaxis rightscatter([1 2 3],[2 5 2],'filled')hold onscatter([1 2 3],[3 4 1],'filled')scatter([1 2 3],[4 2 4],'filled')hold offcolororder({'r','b','c'})legend

Create chart with two y-axes (8)

Add Second y-Axis to Specific Axes

Open Live Script

Starting in R2019b, you can display a tiling of plots using the tiledlayout and nexttile functions. Call the tiledlayout function to create a 2-by-1 tiled chart layout. Call the nexttile function to create the axes objects ax1 and ax2. Add a second y-axis to the top axes by specifying ax1 as the first input to yyaxis. If you do not specify the axes, then yyaxis adds a second y-axis to the current axes.

x = linspace(1,10);tiledlayout(2,1)% Top plotax1 = nexttile;yyaxis(ax1,'left')plot(ax1,x,sin(x))yyaxis(ax1,'right')plot(ax1,x,exp(x))% Bottom plotax2 = nexttile;plot(ax2,1:10)

Create chart with two y-axes (9)

Input Arguments

collapse all

axTarget axes
current axes (default) | Axes object

Target axes, specified as an Axes object. Ifyou do not specify an Axes object, then yyaxis usesthe current axes.

Limitations

  • When working with two y-axes, you cannot:

    • Rotate the axes (2-D view only).

    • Pin annotations.

    • Copy the axes object using copyobj.

Tips

  • To determine which side of the axes is active, querythe YAxisLocation propertyfor the Axes object. The property is set to 'left' whenthe left side is active and 'right' when the rightside is active. The YAxisLocation property foran Axes object with two y-axesis read only.

  • To clear the active side, use cla.To clear both sides of the axes and remove the right y-axis,use cla reset. Alternatively, you can wait to clearboth sides and remove the right y-axis untilthe next plotting command by setting the NextPlot propertyof the Axes object to 'replaceall'.

  • The Children property of the Axes object only contains the children for the active side. To access all the children for both sides, use the allchild function.

Algorithms

collapse all

Grid Lines

Grid lines correspond with the tick mark locations along the left y-axis.

Colors and Line Styles

Plots associated with a particular side of the axes use thesame color as the y-axis on that side. If a sidecontains multiple lines, then the lines cycle through the line styleorder. The left y-axis uses the first color inthe color order of the Axes object, and the right y-axisuses the second color.

If you add a second y-axis to an Axes objectthat contains charts, then the existing charts and the left y-axisdo not change colors. The right y-axis uses thenext color in the color order.

Axes Properties

Axes properties related to the y-axis have two values. However, MATLAB® gives access only the value for the active side. For example, if the left side is active, then the YLim property of the Axes object contains the limits for the left y-axis. However, if the right side is active, then the YLim property contains the limits for the right y-axis.

An exception is that the YAxis property of the Axes object contains an array of two ruler objects (one for each y-axis). You can use the rulers to access each y-axis without depending on the active side. For an example, see Modify Properties of Charts with Two y-Axes.

Version History

Introduced in R2016a

See Also

plot | stem | stairs | bar | hold | cla

Topics

  • Create Chart with Two y-Axes
  • Modify Properties of Charts with Two y-Axes

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.

Create chart with two y-axes (10)

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

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Create chart with two y-axes (2024)

References

Top Articles
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 5601

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.