File Exchange

image thumbnail


version (967 Bytes) by Zhipeng Gao
A plot function much more powerful than plotyy


Updated 24 May 2006

No License

Editor's Note: This file was a File Exchange Pick of the Week

When we use matlab function plotyy, it's inconvenient to setup the labels and ranges of the three axes and the legend of the two lines. What's more, plotyy can not be used in some cases, for example,if the target axes is a child of a uipanel. layerplot enables one who has little knowledge about the graphics handle to plot two lines with two y-axis in one figure and easily setup the corresponding axes properties.

Cite As

Zhipeng Gao (2021). layerplot (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (19)


Hi. someone could explain me how i do use this function?

I have no idea to use it. An example please.

I don't speak english very well.

Rubakumar Sankararaj

Jeff ahrens

Nice simple program. Just what I needed.



Great Little Programe - i've lost hours of my life playing with plotyy!!!

I have a question though. I'm using the edited version of the code as per Zhipeng Gao (25 May 2006) but i have a problem plotting the x-axis labels.

Here is my code.
plot_dates = X0_TS.dates('01-Jul-1980::01-Jan-2008');

[hAxes,hLine] = layerplot(plot_dates,fts2mat(hprx_reg.MAT5),plot_dates,fts2mat(X0_reg.IPRO),...
{'Date','Excess Hold Period Return','Difference in Belief'},...
[datenum(1980, 07, 01),datenum(2010, 07, 01); -0.2,+0.2; 0,+3],...
{'Excess Hold Period Return','Difference in Belief'});

datetick('x', 10)


X0.TS is a financial time series object which I extract the dates from. I would like to do something like :

x_ticks = [datenum(1980, 07, 01) datenum(1985, 07, 01) datenum(1990, 07, 01) datenum(1995, 07, 01) datenum(2000, 07, 01) datenum(2005, 07, 01) datenum(2010, 07, 01)];

to specify a common date range for both series. My code above works but but not properly. If I include x_ticks in set(hAxes,'XTick',[]) I get multiple labels on the x-axis?

Is there a simply way of doing this?

Many thanks

Nathanael Yoder

Has issues with resizing

Nathanael Yoder

Byron Williams

jon e

ps. to make compatible with subplotting two lines need modification.
figure(...) should be commented out
haxes(1) = axes;
should be changed to haxes(1) = gca;

jon e

Nice work. One suggestion is to not have this function automatically plot into a new figure.
For instance, let's say you want to use subplot to have 4 figures all with layerplot, as is, this function can't handle it.
an easy modification to make

Ariell Reshef

Francisco Lopes

It's seems to be perfect!!

Doug Dusini

I think the author did a very nice job of presenting a solution to making nice yyplots. The coding is a bit cumbersome however. One problem I would like to see addressed is the use of common grid spacing in the y axes so that one plot grid can be used for bothe axes.

Mukhtar Ullah

Nice work. The syntax goes againts the spirit of Matlab. Who can remember to give some arguments in a cell and others in a matrix. Here is the example given in LAYERSPLOT documentation, using proper MATLAB syntax:

[ax,h(1),h(2)] = plotyy(t,y1,t,y2,'plot');
xlabel(ax(1),'Time (t)');
legend(h,'y = sin(t)','y = 100*cos(t)')
axis(ax(1),[0 2*pi -2 2])
axis(ax(2),[0 2*pi -200 200])

kk zz

It's so excellent. I admired the author very much.

dodo xing

It is very convenient,good idea ,thank you very much!

zhou xianove

it's good.

Zhipeng Gao

I can't update layerplot at present.So, I'd like to do some modification in the comments. Now see the following:

hLine(1) = plot(x1,y1,'b');
hAxes(1) = gca;
hAxes(2) = axes('Parent',get(hAxes(1),'Parent'),'Position',get(hAxes(1),'Position'),'YColor','r');
hLine(2) = plot(x2,y2,'r');
if nargin >= 5
if nargin >= 6
if nargin ==7, legend(hLine,Legends{:}); end;

James Richard

It's indeed helpful.Thanks!

Rick Barnes

Replace "hLine(1) = line(x1,y1);" with "hLine(1) = line(x1,y1,'Color','b');"
in case the default color order does not start with blue. Otherwise, neat!

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!