File Exchange

image thumbnail

addaxis

version 1.1 (21.4 KB) by

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

4.59524
49 Ratings

128 Downloads

Updated

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

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.

Comments and Ratings (71)

poleposition

Nate Horn

trying to understand how to use this code while importing excel data. right now i am running the example code and trying to understand what is happening. i get these errors.

Error using matlab.graphics.axis.Axes/get
There is no colorord property on the Axes class.

Error in aa_splot (line 13)
cord = get(gca,'colorord');

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

Error in addplottest (line 3)
   addaxis(x,sin((x-pi)/3));
 
the code i am trying to relate it to is
Forward = xlsread('MATLABsheet','Forward','U3:AD18')
%1 is Forward, 2 is Radial, and # is Reverse (A=TDH B=BH C=eff%
x= Forward(:,1);

y1= Forward(:,4);
y2= Forward(:,7);
y3= Forward(:,10);

plot(x,y1);
addaxis(x,y2);
addaxis(x,y3);

addaxislabel(1,'one');
addaxislabel(2,'two');
addaxislabel(3,'three');

i dont understand the rest though

Nate Horn

Great file.
I was wondering if there is a way to set a variable to addaxis like we do to plot.
for example:
p1 = plot(t, data, 'displayname', 'Data1')

flav

flav (view profile)

Thank you for this, it works great! However I was looking for the same in X-axis. Is it possible to adapt it for the x axis ?

Another modification would be to stick the first added axis with the right side of the figure instead of leaving a small space. I would find it visually cleaner.

Vidz

Vidz (view profile)

Al

Al (view profile)

Hi,

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

Great function!

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

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?

wenzhu wang

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

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

Ha Sen

Ha Sen (view profile)

Hi,

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

JEREMIE

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

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.

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.

Bobi

Bobi (view profile)

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

Mikko Pesola

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

aadwindowresizefcn.m:
Change "if ax_manage" to
"if isa(ax_manage,'matlab.graphics.axis.Axes')"

Jan

Jan (view profile)

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

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

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.

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?

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?

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? http://www.mathworks.com/matlabcentral/answers/158068-hide-legend-entries-with-addaxis-function

Edward

Edward (view profile)

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

Elisa

Elisa (view profile)

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!

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.

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?

Thanks

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

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.

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

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!

Priyesh Gosai

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

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

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

LONG SHEN

Flo

Flo (view profile)

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?

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" :(

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)?

Ralph

Ralph (view profile)

Chad Greene

Chad Greene (view profile)

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

P K

P K (view profile)

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

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.

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.

hoan

hoan (view profile)

Great tool!

Gaurav Gupta

Gaurav Gupta (view profile)

Erik

Erik (view profile)

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

Does anyone have a workable solution?

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

John

John (view profile)

John

John (view profile)

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

Ibrahim M

Great work!

Ibrahim M.

Andres

Andres (view profile)

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

Ian

Ian (view profile)

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

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.

Amir Abbas Zadpoor

Well done! Thanks a lot

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.

Wang Ding

a very useful tool! Thanks a lot

Xianyu Zhao

Very useful, thanks a lot

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.

beckali abdel

useful, thanks

Patrick Celka

Best to fix the plotyy bug

fabiano busdraghi

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

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.

Neelu D

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

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

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

1.1

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

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

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

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

Added support for R14SP2

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

MATLAB Release
MATLAB 7.10 (R2010a)
Acknowledgements

Inspired: addaxes.m v1.1 (Sep 2009), Axis XX

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

» Watch video