File Exchange

image thumbnail

addaxis

version 1.1.0.0 (21.4 KB) by Harry Lee
Add multiple y-axes to plots. zoomable with zoom modification.

186 Downloads

Updated 07 Jul 2016

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 (110)

Ellyn Gray

Does anyone know why the zip file won't open? I've had no proble downloading other packages today. My computer says "Error 1 -- Operation not permitted"

hima nik

Liam Smith

When using GUIDE you have to make sure your Axes uses 'normalized' for the units (GUIDE sets this to 'characters'): set(handles.axes1, 'Units', 'normalized');

Index in position 1 exceeds array bounds (must not exceed 1).

Error in aa_splot (line 32)
set(gca,'Color',cord(mod([0:6]+1,7)+1,:));

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

I am also getting error 'Error using matlab.graphics.axis.Axes/get
There is no colorord property on the Axes class.'
I changed 'colorord' with 'ColorOrder' with reviews of sir Antino Rufo, but now I am getting error
Error using matlab.graphics.axis.Axes/get
There is no ColorOrderer property on the Axes class.
I am using MATLAB2018b

Great code. Is there a way to specify the direction of each axis? It seems that all values much increase going up the y-axis. However, I have data that both increase and decrease, and whenever I reverse the y-direction it essentially flips the entire plot. Thanks.

Found the error. In the file 'aa_splot.m' you must replace everything that says 'colorord' with 'ColorOrder'. Everything works after that.

I am using 2017b and i get the following error 'Error using matlab.graphics.axis.Axes/get
There is no colorord property on the Axes class.'

Is there any way I can move one of the axes to be in another X position instead of at the ends?

Thanks

Manyi Ho

When I ran the following code:
semilogy(x1, y1)
addaxis(x2, Y2)
addaxis(x3, y3)
addaix(x4, Y4)

However the y scale in Y4 is messed up and didn't reflect the true value. When I change the semilogy to just plot(x1, Y1), then everything is fine. Any solution? Thank you!!

I have been playing with a way to get all axis to one side or the other. It seems that there is no parameter to state which side of the graph you would like your new Y axis to show on. This makes for some pretty ugly graphing, in my opinion.

It is working well for me. However, is there a way to control the offset of the axis? My output currently has two y-axes on one side, but they are more or less on-top of each other. I need to add sufficient spacing to include the axis titles and maintain readability.

I am getting this error still after following the comments

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

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

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

Works beautifully after the fix stated in the comments!

Working great on R2016a after applying Keller's 'colorord' to 'colororder' fix.

Harsh Singh

Harsh Singh

I am getting wrong values for an added axis when I use the data cursor in the plot. Any ideas on how to rectify this?

Using R2016b, same problem occured using this set of functions.

Here´s our solution, rather simple changes are needed:

Look-up line "lastColor = get(oldplots(1),'colorord');", I guess its line 16.Change used property from "colorord" to "Color".

Details:
Problem occures when "addaxis.m" calls "aa_splot" in line 135 by "hplts = aa_splot(varargin{:});"
Structure "if" shall compare last used "Color" from variable "oldplots" with upcoming plot color due to "ColorOrder".
I´d furthermore suggest to change all remaining "colorord" to "ColorOrder" for more simple reading.

regards, Oliver

how to add our x and y axis values in program?

Is it possible to use area or scatter plots on these added axes?

Lei Zheng

Alysia Herr

Within aa_splot, you can comment out the ' set(gca,'colororder',cord(mod([0:6]+1,7)+1,:)); ' (both times it appears) if Jessica Keller's fix doesn't work. Then change colors manually.

Create an axis and line handles by changing the very first line of the addaxis.m function to:
[h a]=addaxis(varargin)
And adding this line to the very end of the addaxis.m code:
if nargout>0, h = hplts, a = h_ax; end

Then you can adjust color of the line using the 'h' handle (i.e., h.Color='k') and adjust the color of the axis using the 'a' handle (i.e. a.YColor='k'.

Even after changing colorord to colororder I still get this issue (2016a)

Index exceeds matrix dimensions.

Error in aa_splot (line 18)
set(gca,'colororder',cord(mod([0:6]+1,7)+1,:));

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

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 135)
hplts = aa_splot(varargin{:});

It probably works well most of the time but I'm getting this issue (2016a)

How can i change the fontsize of the additional axes?

The userdata in the axes handle is empty so I am unable to manipulate the different axes (e.g. set one axes reverse). How can I obtain the axeshandles of the different axes?

I'm getting this error:

Undefined function or variable 'getaddaxisdata'.

Error in addaxis (line 66)
axh = getaddaxisdata(cah,'axisdata');

Does anyone know how to get around this?

Yep, on MATLAB R2017b, following the suggestion of Jessica Keller, it really works like a charm. Thank you!!!

Kelvin

Thanks Jessica Keller for the tip on colorord!!

If u have the error: 'There is no colorord property on the Axes class. go into aa_splot and replace all colorord with colororder commands. it worked for me with 2017 version.

I'm having problems with zooming the plot:
1. With a single click zoom, I zoom "all the way" in both axes.
2. With the rbbox I can zoom X-Axis partially, but Y-Axis "all the way".
3. Using "zoom ON" or "zoom XON", doesn't make a difference with the X-Axis zoom abilities.
4. I'm using R2014a
Pleas assist.

To be more precise:
1. With a single click zoom, I zoom "all the way" in both axes.
2. With the rbbox I can zoom X-Axis partially, but Y-Axis "all the way".
3. Using "zoom ON" or "zoom XON" doesn't make a difference. in X-Axis zoom abilities.
4. I'm using R2014a
Pleas assist.

clks

This is the error code:
Index exceeds matrix dimensions.

Error in aa_splot (line 18)
set(gca,'ColorOrder',cord(mod([0:6]+1,7)+1,:));

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

Error in two_plots (line 19)
addaxis(x,y3);

clks

Hi, the solution to the problem 'There is no colorord property on the Axes class. does not help me. Have you got an other suggestion?

Hi,
solution to the problem
'There is no colorord property on the Axes class.

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

Go into the file aa_splot and replace 'colorord' with 'ColorOrder' (3 times).
This solved the problem for me.

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

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

Al

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

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

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

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.

MichMichel

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

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

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

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

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

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

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

John

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

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

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

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

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

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

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

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

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

Ralph

Chad Greene

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

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

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.

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

Great tool!

Gaurav Gupta

Erik

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?

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.

John

John

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

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

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

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

1.0.0.0

Fixed small bug in addaxisplot so it works to add plots to the original axis, also addaxislabel input made more flexible.

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 Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

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