Code covered by the BSD License  

Highlights from
addaxis

4.63333

4.6 | 31 ratings Rate this file 205 Downloads (last 30 days) File Size: 20.6 KB File ID: #9016
image thumbnail

addaxis

by

 

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
Description

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.

Acknowledgements

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

MATLAB release MATLAB 5.3.1 (R11.1)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (44)
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)
set(gca,'colorord',cord(mod([0:6]+1,7)+1,:));
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.

30 Oct 2013 Renato

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?

Thanks

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;
plot(x,sin(x));

addaxis(x,sin(x-pi/3));
addaxislabel(1,'one');
addaxislabel(2,'two');

legend('one','two');

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

Error in ==> addaxis at 137
setaddaxisdata(cah,axh,'axisdata');

Error in ==> test at 3
addaxis(x,sin(x-pi/3));"

21 Sep 2013 Dan

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

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

29 Apr 2013 Tobias

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;
set(h_new,'ActionPostCallback',@postcallbackzoom);
set(h_new,'ActionPreCallback',@precallbackzoom);
set(h_new,'Enable','on');
set(h_new,'Enable','off');
h_new=pan;
set(h_new,'ActionPostCallback',@postcallbackpan);
set(h_new,'ActionPreCallback',@precallbackpan);
set(h_new,'Enable','on');
set(h_new,'Enable','off');

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');
if(~isempty(leg))
ax = ax(ax~=findall(obj,'Tag','legend'));
end
for i=1:length(ax)
old(i+1,1:2)=get(ax(i),'YLim');
old(i+1,3)=ax(i);
end
setappdata(obj,'OldAxes',old);

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

function precallbackpan(obj,evd)
ax = findall(obj,'type','axes');
ax = ax(ax~=evd.Axes);
leg = findall(obj,'Tag','legend');
if(~isempty(leg))
ax = ax(ax~=findall(obj,'Tag','legend'));
end
OldY=[get(gca,'YLim'),ax'];
set(obj,'WindowButtonMotionFcn',{@CallbackPan,obj});
setappdata(obj,'OldY',OldY);

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

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

GREAT!

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? martijnvanhooff89@gmail.com

30 Nov 2012 LONG SHEN  
18 Sep 2012 Flo

Erik:http://www.mathworks.com/matlabcentral/newsreader/view_thread/292235, thank you erik!

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

11 Sep 2012 Flo

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

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

05 May 2012 Ralph  
13 Mar 2012 Chad Greene  
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:
http://mathworks.com/matlabcentral/answers/28436-plotyy-with-sync-yaxis-tics-on-both-sides

05 Feb 2012 P K

I figured it out!
I just used the following commands:
AX=findall(0,'type','axes');
set(AX(2),'fontsize',16,'fontweight','bold')

05 Feb 2012 P K

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.

06 Oct 2011 hoan

Great tool!

26 Sep 2011 Gaurav Gupta  
06 Oct 2010 Erik

I've answered my own question:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/292235

Best,

Erik

13 Sep 2010 Erik

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 http://www.mathworks.com/support/solutions/en/data/1-31Q3HK/?solution=1-31Q3HK to no avail.

Does anyone have a workable solution?

22 Nov 2009 Ben

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 plot...one 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.

18 Aug 2009 John  
18 Aug 2009 John

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

07 Aug 2009 Ibrahim M

Great work!

Ibrahim M.

30 Jul 2009 Andres

A good multiple y-axis tool - it could be a bit more customizable. Use linkzoom ( http://www.mathworks.com/matlabcentral/fileexchange/21414 ) to avoid the zoom modification, or use Carlos Adrian's derivative (not tested yet).

01 Jul 2009 Ian

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

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
fabiano

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.

Updates
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.

Contact us