Multiple y axes on single x axis (2024)

577 views (last 30 days)

Show older comments

Umang Dongre on 30 Apr 2019

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis

Commented: Rik on 14 Mar 2022

Accepted Answer: Adam Danz

Open in MATLAB Online

Hello, I want to know how to do three y axis one next to one on left side with space. i'm trying but not getting it properly.

VarName1=A0006_charge(:,1);

VarName2=A0006_charge(:,2);

VarName3=A0006_charge(:,3)/3600;

VarName4=A0006_charge(:,4);

plot(VarName3, VarName1, VarName3, VarName2, VarName3, VarName4);

ylim([-1 5])

Multiple y axes on single x axis (2)

Like this.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Adam Danz on 30 Apr 2019

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#answer_372987

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#answer_372987

Edited: Adam Danz on 26 May 2020

Open in MATLAB Online

Try a function from the file exchange

  • https://www.mathworks.com/matlabcentral/fileexchange/9016-addaxis
  • https://www.mathworks.com/matlabcentral/fileexchange/1017-plotyyy

Or adapt this demo to your needs (yyaxis requires r2016a or later)

Setting up two y axes is trivial. Three y axes, not so trivial. You'll need to set up a plot with and right and left y axes (using yyaxis) and then you'll need to overlay invisible axes on top of the original ones, perfectly placed, and perfectly scaled so the vertical and horizontal ticks align. Finally, add some space to the right of the y tick labels so they are horizontally offset.

Here's a demo that you can adapt to your needs. The critical steps are setting the axis ticks and making sure the spacing is the same between axes. I use the grid feature for both axes to ensure that they are overlayed property (otherwise you'll see double grids).

% Create some data to work with

x = 0:.1:3;

y1 = exp(x);

y2 = log(x);

y3 = x.^2;

% Plot on the left and right y axes

figure

ax1 = axes;

yyaxis left % see [1]

plot(x,y1)

pause(0.1) % see [3]

% set the y(left) and x tick values, make them permanent

% This is the tricky part and shoudl receive a lot of thought when

% you adapt this to your code...

ax1.XTickMode = 'manual';

ax1.YTickMode = 'manual';

ax1.YLim = [min(ax1.YTick), max(ax1.YTick)]; % see [4]

ax1.XLimMode = 'manual';

grid(ax1,'on')

ytick = ax1.YTick;

yyaxis right % see [1]

plot(x,y2)

% create 2nd, transparent axes

ax2 = axes('position', ax1.Position);

plot(ax2,x,y3, 'k')

pause(0.1) % see [3]

ax2.Color = 'none';

grid(ax2, 'on')

% Horizontally scale the y axis to alight the grid (again, be careful!)

ax2.XLim = ax1.XLim;

ax2.XTick = ax1.XTick;

ax2.YLimMode = 'manual';

yl = ax2.YLim;

ax2.YTick = linspace(yl(1), yl(2), length(ytick)); % see [2]

% horzontally offset y tick labels

ax2.YTickLabel = strcat(ax2.YTickLabel, {' '});

% [1] https://www.mathworks.com/help/matlab/ref/yyaxis.html

% [2] this is the critical step to align the grids. It assumes both

% axes contain ticks at the start and end of the y axis

% [3] For some reason when I step through the code, the plots appear

% as they should but when I run the code at it's natural speed

% there are graphics issues. It's as if code execution is

% ahead of the graphics which is annoying. A brief pause

% fixes this (r2019a)

% [4] Scaling is easier if the ticks begin and end at the axis limits

Multiple y axes on single x axis (4)

Changes needed to create the double y-axis on the right side instead of the left side

  1. Calculate ytick = ax1.YTick after plotting on the right axis.
  2. After creating ax2, set the y axis location to the right side using ax2.YAxisLocation = 'right';
  3. Pad the left side of the ax2 y-axis ytick lables instead of the right side by changing: ax2.YTickLabel = strcat({' '},ax2.YTickLabel);

For matlab releases prior to 2016a, use plotyy() instead

The code above has been adapted to plotyy().

% Create some data to work with

x = 0:.1:3;

y1 = exp(x);

y2 = log(x);

y3 = x.^2;

% create the first axes with the two y axes

figure

yyh = plotyy(x,y1,x,y2); %see [1]

yyh(1).XTickMode = 'manual';

yyh(1).YTickMode = 'manual';

yyh(1).YLim = [min(yyh(1).YTick), max(yyh(1).YTick)]; % see [4]

yyh(1).XLimMode = 'manual';

grid(yyh(1),'on')

ytick = yyh(1).YTick;

% create 2nd, transparent axes

ax2 = axes('position', yyh(1).Position);

plot(ax2,x,y3, 'k')

pause(0.1) % see [3]

ax2.Color = 'none';

grid(ax2, 'on')

% Horizontally scale the y axis to alight the grid (again, be careful!)

ax2.XLim = yyh(1).XLim;

ax2.XTick = yyh(1).XTick;

ax2.YLimMode = 'manual';

yl = ax2.YLim;

ax2.YTick = linspace(yl(1), yl(2), length(ytick)); % see [2]

% horzontally offset y tick labels

ax2.YTickLabel = strcat(ax2.YTickLabel, {' '});

% [1] https://www.mathworks.com/help/matlab/ref/plotyy.html

% [2] this is the critical step to align the grids. It assumes both

% axes contain ticks at the start and end of the y axis

% [3] For some reason when I step through the code, the plots appear

% as they should but when I run the code at it's natural speed

% there are graphics issues. It's as if code execution is

% ahead of the graphics which is annoying. A brief pause

% fixes this (r2019a)

% [4] Scaling is easier if the ticks begin and end at the axis limits

15 Comments

Show 13 older commentsHide 13 older comments

Umang Dongre on 6 May 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_702174

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_702174

  • A0006.zip

Thank you, for responding on my question :) sir i tried your answer but not getting it properly.

Sir,

I'm sending you "A0006.mat" file, Will you please help me to plot that data into 3 - y axes.

Thanks in advance.

Adam Danz on 6 May 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_702195

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_702195

Edited: Adam Danz on 6 May 2019

Your zip files doesn't contain your code. You'll need to provide the section of code that does your plotting.

Umang Dongre on 8 May 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_703101

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_703101

Edited: Umang Dongre on 8 May 2019

Open in MATLAB Online

Code -

VarName1=A0006(:,1); % Voltage

VarName2=A0006(:,2); % Current

VarName3=A0006(:,3)/3600; % Time

VarName4=A0006(:,4); % SOC

plot(VarName3, VarName1, VarName3, VarName2, VarName3, VarName4);

I want to plot this voltage, current and SOC with respect to Time, And in my data set A0006.mat there are 4 colomns 1st-voltage, 2nd-current, 3rd-time, 4th-SOC

Adam Danz on 8 May 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_703115

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_703115

Edited: Adam Danz on 8 May 2019

Open in MATLAB Online

Your "x" variable is VarName3. The other 3 variables are your 'y' values. Now you just need to apply that to my method.

x = A0006(:,3)/3600;

y1 = A0006(:,1);

y2 = A0006(:,2);

y3 = A0006(:,4);

% and the rest of the code in my answer.

If you have any other difficulties you'll need to share your version of the code and describe why it's not working.

Umang Dongre on 10 May 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_703836

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_703836

Open in MATLAB Online

yes I did but it shows error

Undefined function 'yyaxis' for input arguments of type 'char'.

Error in OCV_SOC (line 7)

yyaxis left

and in figure window it does not showing anything.

code:

y2=A0006(:,1);

y1=A0006(:,2);

x=A0006(:,3)/3600;

y3=A0006(:,4);

figure

ax1 = axes;

yyaxis left

plot(x,y1)

pause(0.1)

ax1.XTickMode = 'manual';

ax1.YTickMode = 'manual';

ax1.YLim = [min(ax1.YTick), max(ax1.YTick)];

ax1.XLimMode = 'manual';

grid(ax1,'on')

ytick = ax1.YTick;

yyaxis right

plot(x,y2)

ax2 = axes('position', ax1.Position);

plot(ax2,x,y3, 'k')

pause(0.1)

ax2.Color = 'none';

grid(ax2, 'on')

ax2.XLim = ax1.XLim;

ax2.XTick = ax1.XTick;

ax2.YLimMode = 'manual';

yl = ax2.YLim;

ax2.YTick = linspace(yl(1), yl(2), length(ytick));

ax2.YTickLabel = strcat(ax2.YTickLabel, {' '});

Adam Danz on 10 May 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_703847

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_703847

yyaxis() was introduced in r2016a. If you're using a release earlier than that, you'll need to use plotyy(). I'll update my answer now with an example using plotyy()

Umang Dongre on 20 May 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_706929

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_706929

yes please ... I am using r2014a that's why it doesn't working.

Adam Danz on 20 May 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_706968

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_706968

Ok, scroll down and see the 2nd half of my answer for a plotyy() example.

Umang Dongre on 21 May 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_707265

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_707265

Open in MATLAB Online

Ok, so output has came like this i want that blue curve i.e. data "y1" little lower than green one.

Multiple y axes on single x axis (14)

and "y3" hasn't plot. one error occured

Structure assignment to non-structure object.

Error in OCV_SOC (line 8)

yyh(1).XTickMode = 'manual';

and data of "x" is changing after code runs.

Adam Danz on 21 May 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_707321

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_707321

Open in MATLAB Online

So use ylim() to scale the left y-axis so your blue curve is lower in the axis.

Perhaps the XTickMode property hasn't been released yet in whatever matlab release you're using. Try replacing it with (not tested):

set(yyh(1), 'XTick', get(yyh(1), 'XTick'))

Note that this property is set in several lines of my code so you'll need to make that change a few times.

Umang Dongre on 27 May 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_709020

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_709020

Open in MATLAB Online

Ok

y2=A0006(:,1);

y1=A0006(:,2);

x=A0006(:,3)/3600;

y3=A0006(:,4);

% create the first axes with the two y axes

figure

yyh = plotyy(x,y1,x,y2); %see [1]

ylim([-1 5])

set(yyh(1), 'XTick', get(yyh(1), 'XTick'))

set(yyh(1), 'YTick', get(yyh(1), 'YTick'))

% yyh(1).XTickMode = 'manual';

% yyh(1).YTickMode = 'manual';

yyh(1).YLim = [min(yyh(1).YTick), max(yyh(1).YTick)]; % see [4]

yyh(1).XLimMode = 'manual';

grid(yyh(1),'on')

ytick = yyh(1).YTick;

% create 2nd, transparent axes

ax2 = axes('position', yyh(1).Position);

plot(ax2,x,y3, 'k')

pause(0.1) % see [3]

ax2.Color = 'none';

grid(ax2, 'on')

% Horizontally scale the y axis to alight the grid (again, be careful!)

ax2.XLim = yyh(1).XLim;

ax2.XTick = yyh(1).XTick;

ax2.YLimMode = 'manual';

yl = ax2.YLim;

ax2.YTick = linspace(yl(1), yl(2), length(ytick)); % see [2]

% horzontally offset y tick labels

ax2.YTickLabel = strcat(ax2.YTickLabel, {' '});

1 error is occuring

Improper index matrix reference.

Error in OCV_SOC (line 13)

yyh(1).YLim = [min(yyh(1).YTick), max(yyh(1).YTick)]; % see [4]

and sir what about "y3" data it is not getting plot.

Adam Danz on 28 May 2019

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_709379

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_709379

Have you looked into the error at all? Have you tried to understand what's wrong? What is the value of yyh(1)? Is that causing the error? Is yyh empty? If yyh(1) does not cause an error, does yyh(1).YTick cause an error? I don't have values for A0006 so I cannot run your code.

Sarah Poole on 11 Feb 2022

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_1979765

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_1979765

thank you, this demo works great even on R2021a. :)

Adam Danz on 11 Feb 2022

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_1980980

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_1980980

Thanks for the feedback, Sarah.

Rik on 14 Mar 2022

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_2040194

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#comment_2040194

Comment posted as flag by @Mohsin Tariq:

the simplest answer i have found

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsAxis Labels

Find more on Axis Labels in Help Center and File Exchange

Tags

  • matlab
  • axes
  • graph
  • yaxis
  • yyaxis
  • plotyy
  • 3 y axes
  • three y axes

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Multiple y axes on single x axis (21)

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

Multiple y axes on single x axis (2024)
Top Articles
Latest Posts
Article information

Author: Allyn Kozey

Last Updated:

Views: 6188

Rating: 4.2 / 5 (63 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Allyn Kozey

Birthday: 1993-12-21

Address: Suite 454 40343 Larson Union, Port Melia, TX 16164

Phone: +2456904400762

Job: Investor Administrator

Hobby: Sketching, Puzzles, Pet, Mountaineering, Skydiving, Dowsing, Sports

Introduction: My name is Allyn Kozey, I am a outstanding, colorful, adventurous, encouraging, zealous, tender, helpful person who loves writing and wants to share my knowledge and understanding with you.