View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

4.7 | 46 ratings Rate this file 105 Downloads (last 30 days) File Size: 21.4 KB File ID: #9016 Version: 1.1
image thumbnail



Harry Lee (view profile)


12 Nov 2005 (Updated )

Add multiple y-axes to plots. zoomable with zoom modification.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

Adds multiple axes to plots using addaxis.m. Automatically adjusts axes to accomodate as many axes as you want.
Add more plots to any added axes with addaxisplot.m
Add y-labels to any of the axes with addaxislabels.m

All plots zoom together. A small modification to the zoom function allows all of the added axes to update with zoom.

zoom modifications for R11.1 and R14SP2 are included.

Usage is almost exactly like the plot command, see the help for addaxis.m for examples.


This file inspired Addaxes.M V1.1 (Sep 2009) and Axis Xx.

MATLAB release MATLAB 7.10 (R2010a)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (66)
11 Jan 2017 Vidz

Vidz (view profile)

20 Dec 2016 Janapati JayaLakshmi

19 Dec 2016 Al

Al (view profile)


How can I move the axis label along with the plot itself?
I have 2 different subplots with addaxis function, but their orientation does not match, so I need to set it manually
I can modify the axislabel, but the plot itself will not change

22 Sep 2016 Kristyna Kantnerova

Great function!

Can I somehow change the number of ticks on additional axes please? Thanks.

08 Jul 2016 wenzhu wang

When I copy the figure to word, there will be a gray bar on the added y-axises, how can I remove them?

Comment only
08 Jul 2016 wenzhu wang

This is just what I need! Multiple axes and zoom in or zoom out.

Comment only
14 Jun 2016 Mehmet

Mehmet (view profile)

Works great! I just had to remove the colorcord variable and realated ones. It kept giving error for "exceeded matrix index". It worked just fine without it.I only used one extra y axis though.

19 May 2016 Ha Sen

Ha Sen (view profile)


I have updated to R2016a. By using addaxis I get this error, Could someone give an advice? I am trying to plot two bars and a line with the addaxis.

Error in addaxis (line 131)
hplts = aa_splot(varargin{:});

Error in bar_line (line 41)
addaxis (dn, ComMean(:,1));

Comment only
18 Mar 2016 JEREMIE

Thanks for this nice tool.
Is it possible to use addaxis to add an axis on x-axis ?

Comment only
22 Jan 2016 Amy

Amy (view profile)

Is there a way to add multiple lines to the same axes? For instance, I have current, power, and voltage for 2 different things. I need 3 different axes but I want 2 lines per.

13 Jan 2016 Ford Creighton

Is there a way to compact the axes a little more horizontally? Having 5 or 6 axes causes the plot are to become small.

Also, any plans on tweaking for 2015a or 2015b?

Thanks for the great work.

26 Oct 2015 Bobi

Bobi (view profile)

Thanks a loooooooooooooot .... Plotyy sucks totally ! Resolve all the problem of 2d Axes, very very nice :)
Thank you so much.

07 Sep 2015 Mikko Pesola

I got it working with 2014b with following changes:
Change 'colorord' to 'ColorOrder'

Change "if ax_manage" to
"if isa(ax_manage,'')"

Comment only
17 Apr 2015 Jan

Jan (view profile)

Great! Only on Matlab 2014a I had to rename in aa_splot.m "colorord -> ColorOrder".

01 Apr 2015 Florian Haase

The paths mentioned the readme.txt doesn't exists so I can't replace them by the files comes with the download.

23 Mar 2015 Allan

Allan (view profile)

Great work !!
Very Useful. Anyone knows how to reverse a specific y axis? With ax=plotyy() you select the axis ax(1) or ax(2) and reverse it with 'ydir' property. But with addaxis i'm not getting it.

26 Dec 2014 Khanh

Khanh (view profile)

Thank you so much Harry. It is very useful. By the way, I want to plot with multiple x axes instead of y axes. May I know to modify your files to work as I want?

Comment only
05 Dec 2014 John

John (view profile)

For some reason this function causes a figure with visible = off to become visible. I can't figure out why from the code. Any ideas?

Comment only
09 Oct 2014 Edward

Edward (view profile)

Great function. Having trouble with the legend display for a corner case. Does anyone have a solution to the following problem?

09 Oct 2014 Edward

Edward (view profile)

20 Sep 2014 John

John (view profile)

answered my own question. use the function below to get a handle for the new axis, then adjust like so:

set(haxis2,'Position',[ 0.90 0.1100 0.001 0.8150]), modifiying the X location (.90) and the width .001).

20 Aug 2014 Elisa

Elisa (view profile)

08 Dec 2013 Ali Hummos

I needed to get a handle for the newly created axis to change its properties. I found it very helpful to edit the first line in the file addaxis.m to be:
function [h_ax] = addaxis(varargin)
so that the function returns a handle to the newly created axis.
Solid work. Thanks!

07 Nov 2013 Zhenjia

My error is always:

Index exceeds matrix dimensions.
Error in aa_splot (line 18)
Error in addaxis (line 131)
hplts = aa_splot(varargin{:});

The size of cord is 1*3, not 7*3. Does any one meet this problem? Thanks.

Comment only
30 Oct 2013 Renato

Renato (view profile)

Hi Harry, great work. Thanks a lot.
I've been searching for a version which make possible to undo addaxis. It means, return to the before status. If I added 3 axis, when undo figure shows the original plot and 2 axis added, etc...
Do you know how can I do it?


29 Oct 2013 bayern science

Great program! But could it be improved such that we can plot the error bars together with the plots?

15 Oct 2013 Charles

I apologize for accidentally rating this function with just one star yesterday, I didn't mean to do that, but I can't figure out how to edit my previous comment or rating.

I did get the code to work today, although I haven't really changed anything. So I don't know what the problem was exactly.

14 Oct 2013 Charles

I just downloaded this function and when running the sample code, I get an error. I have no idea what's going on. Any thoughts?

Sample code:
x = 0:.1:4*pi;



The error reads as follows...
"??? Undefined function or method 'setaddaxisdata' for input arguments of type 'cell'.

Error in ==> addaxis at 137

Error in ==> test at 3

21 Sep 2013 Dan

Dan (view profile)

Has anyone answered Ian's question about making one of the axes log scale? I would like to know how to do this. Thanks!

Comment only
21 Aug 2013 Priyesh Gosai

21 Jul 2013 olumide odeyemi

hi guys, I have been trying to find a way to put 2 right and 1 left y axes in the same legend box before I came across this code. I want to ask if it can put all my objects in one legend box. thanks waiting for your reply and how to go about it

Comment only
29 Apr 2013 Tobias

Tobias (view profile)

Great function!
For those of you who can not figure out how to make the zooming and panning working, here is a solution:

1. Open the addaxis.m and find these lines: "% plot in new axis to get the automatically generated ylimits
hplt = plot(varargin{:});"

2. Add the following code after "hplt = plot(varargin{:});" :
h_new = zoom;

3. Goto the end of 'addaxis.m' and add the followong code:

function precallbackzoom(obj,evd)
old(1:2) = get(evd.Axes,'YLim');
old(3) = evd.Axes;
ax = findall(obj,'type','axes');
ax = ax(ax~=evd.Axes);
leg = findall(obj,'Tag','legend');
ax = ax(ax~=findall(obj,'Tag','legend'));
for i=1:length(ax)

function postcallbackzoom(obj,evd)
old = getappdata(obj,'OldAxes');
new(1:2) = get(evd.Axes,'YLim');
for i=2:s(1)
set(old(i,3),'YLim',[ (old(i,1))+d*d1 , (old(i,2))+d*d2]);
function postcallbackpan(obj,evd)

function precallbackpan(obj,evd)
ax = findall(obj,'type','axes');
ax = ax(ax~=evd.Axes);
leg = findall(obj,'Tag','legend');
ax = ax(ax~=findall(obj,'Tag','legend'));

function CallbackPan(hObject, eventdata, handles)
old = getappdata(hObject,'OldY');
new(1:2) = get(gca,'YLim');
for i=3:length(old)
d1 = get(old(i),'YLim');
d1_= d1(2)-d1(1);

4. That's it. Save your addaxis.m and enjoy zooming and panning. By the way you don't have the use the new zoom-functions (zoom modifications for R11.1 and R14SP2) provided with this function.
Tested on Matlab 2012b.

06 Feb 2013 martijn


but does somebody have the solution to the zoomproblem. The readme does not apply to me, since I use Matlab 2012.

If you know the solution, can you mail me? or drop it here?

30 Nov 2012 LONG SHEN

18 Sep 2012 Flo

Flo (view profile)

Erik:, thank you erik!

Do anybody know how to zoom in AND scale the additional axis and not only the main axis?

Comment only
11 Sep 2012 Flo

Flo (view profile)

Thank you! Does anybody know, how to get the right data cursor values? I only get the y-values from the main-y-axis and not from the "addaxis" :(

08 Aug 2012 eparks

eparks (view profile)

Thanks! Can anyone tell me how to make it so that all additonal axes are added to the same side (opposite the original)?

Comment only
05 May 2012 Ralph

Ralph (view profile)

13 Mar 2012 Chad Greene

Chad Greene (view profile)

11 Feb 2012 Matthias Pospiech

I want to use this tool because plotyy does not provide the option to show the axis ticks onl on one side.

But the second axis on the right side is separated from the actual plot area which looks wrong. And there are not handles, so I have no clue how to modify the style.
And there is no documentation neither in the readme nor in the files so that I can only guess what I have to call.

Any help is welcome to get around all the plotyy problem.

I was linked here from this question:

05 Feb 2012 P K

P K (view profile)

I figured it out!
I just used the following commands:

Comment only
05 Feb 2012 P K

P K (view profile)

Great tool!

I had problems with plotyy and this function solved them all. There is only one thing which I would like to know: how can I change the line thickness, font size, font style on the new axis?
Would appreciate any help.

08 Nov 2011 David C

Works well without zoom. Can someone comment on how to fix the zoom issue? I'm also not sure how to fix the zoom issue with linkzoom. Would appreciate any help.

Comment only
06 Oct 2011 hoan

hoan (view profile)

Great tool!

Comment only
26 Sep 2011 Gaurav Gupta

Gaurav Gupta (view profile)

06 Oct 2010 Erik

Erik (view profile)

I've answered my own question:



Comment only
13 Sep 2010 Erik

Erik (view profile)

Great tool!

Unfortunately I could not get Data Cursor to find the value of a plot at a given point. I tried using Ben's modifications in conjunction with suggestions from to no avail.

Does anyone have a workable solution?

22 Nov 2009 Ben

Ben (view profile)

Great tool! I'm surprised matlab doesn't handle this better natively (the way gnuplot does).

If you need to change linestyles, ytics, etc, just change the declaration of addaxis to:

function [hplts,h_ax] = addaxis(varargin)

This give you back the handle to the plot (as if you called h=plot(...)) and the axes (h=axes(...)) yourself.

The only outstanding issue I have is positioning a legend outside the solution might be to put all the new y-axes on the right (instead of alternating left/right), and then legend Location NorthEast, but I haven't tried that yet.

Comment only
18 Aug 2009 John

John (view profile)

18 Aug 2009 John

John (view profile)

Great Tool. Would be great if you could update the zoom functionality to more recent versions of MATLAB.

Comment only
07 Aug 2009 Ibrahim M

Great work!

Ibrahim M.

Comment only
30 Jul 2009 Andres

Andres (view profile)

A good multiple y-axis tool - it could be a bit more customizable. Use linkzoom ( ) to avoid the zoom modification, or use Carlos Adrian's derivative (not tested yet).

01 Jul 2009 Ian

Ian (view profile)

Hi, good tool.
Is it possible to make some of the additional axis use a log scale?

Comment only
02 Sep 2008 R. J

Hi. This is a great tool, I used it to create something more specific for my needs, so thank you for the ideas. I'm wondering if anyone has figured out a fix to a legend..I can only add to the legend the number of entries on the left side.

18 Aug 2008 Amir Abbas Zadpoor

Well done! Thanks a lot

14 Mar 2008 R Meyer

I think this is a very useful tool, but I guess I have some problems, because there are always error messages when I am using the zoom function. Perhaps it is because I am using the new version of MatLab but I do not know exactly because I have got the software since only a few days. Perhaps anybody can help me.

20 Nov 2007 Wang Ding

a very useful tool! Thanks a lot

16 Aug 2007 Xianyu Zhao

Very useful, thanks a lot

26 Jul 2007 Ian Howat

Great tool. Much^10 easier to use than plotyy. I agree with fabiano that a good modification would be to add multiple horizontal labels.

07 Jul 2007 beckali abdel

useful, thanks

08 Feb 2007 Patrick Celka

Best to fix the plotyy bug

15 Dec 2006 fabiano busdraghi

nice tool. it is too shame that you can not add x axis, that is what i'm looking for

14 Aug 2006 A Pohlmann

I wanted to plot several curves and then add another one on a second y-axis. Tried plotyy and a few different tools and finally used this. Works ok if you don't mess with the orignal axis after adding the second. However, like some other tools it affects the size of subplots with some of them being slimmer or flatter, at random, which means you need to resize manually. Also the second axis is shifted slightly upwards. Other tools seemed to have similar problems, so I wonder whether that has some other cause (thus 4*). Using Matlab 7.0.4 on X11. Nevertheless I get usable graphs.

27 Apr 2006 Neelu D

Best among the posted on the website for adding secondary axis or zooming.

17 Jan 2006 Andrey Loginov

A nice tool thank you,
It is better not redefine splot.m which is used for plotting freq and time resp of system SYS,
Just rename to say splot__.m and use

25 Nov 2005 Johannes Korsawe

A really superb tool. Just what i was about to write on my own. Thank you!

I would just allow the user to choose the color of subsequent plots to the same additional axis (addaxisplot line 43) and not force them to be the same color as the axis.

16 Nov 2005

Removed debugging line in zoom.m that was displaying axis limits.

28 Nov 2005

Added support for R14SP2

29 Nov 2005

In response to suggestion, addaxisplot only changes color to match axis if no color is specified.

27 Dec 2005

*Added feature to allow setting y-limits after plotting.
*Added feature to allow removal of all added axes.
*Improved implementation to avoid using 'userdata'.

15 Feb 2006

changed splot.m to aa_splot.m in response to a user's suggestion.

07 Jul 2016 1.1

Supports zooming of added axes in versions of Matlab that support the zoom mode objects with ActionPostCallback and ActionPreCallback. Tested for R2010a.

Contact us