4.58333

4.6 | 13 ratings Rate this file 68 Downloads (last 30 days) File Size: 2.1 KB File ID: #11178

layerplot

by Zhipeng Gao

 

24 May 2006 (Updated 24 May 2006)

A plot function much more powerful than plotyy

Editor's Notes:

This file was a File Exchange Pick of the Week

| Watch this File

File Information
Description

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.

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (19)
24 May 2006 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!

25 May 2006 James Richard

It's indeed helpful.Thanks!

25 May 2006 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');
% DO NOT USE FUNCTION 'LINE' HERE
hAxes(1) = gca;
set(hAxes(1),'YColor','b','Box','on');
hAxes(2) = axes('Parent',get(hAxes(1),'Parent'),'Position',get(hAxes(1),'Position'),'YColor','r');
hLine(2) = plot(x2,y2,'r');
set(hAxes(2),'Xlim',get(hAxes(1),'Xlim'),'YAxisLocation','right','Color','none','XTickLabel',[],'Layer','top');
if nargin >= 5
    xlabel(hAxes(1),Labels{1},'Color','k');
    ylabel(hAxes(1),Labels{2},'Color','b');
    ylabel(hAxes(2),Labels{3},'Color','r');
end
if nargin >= 6
    if(length(Range(:))==2)
        set(hAxes,'Xlim',Range);
    else
        set(hAxes,'Xlim',Range(1,:));
        set(hAxes(1),'Ylim',Range(2,:));
        set(hAxes(2),'Ylim',Range(3,:));
    end
end
if nargin ==7, legend(hLine,Legends{:}); end;

26 May 2006 zhou xianove

it's good.

26 May 2006 dodo xing

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

27 May 2006 kk zz

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

29 May 2006 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)');
ylabel(ax(1),'sin(t)');
ylabel(ax(2),'100*cos(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])

30 May 2006 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.

07 Sep 2006 Francisco Lopes

It's seems to be perfect!!

30 Jun 2007 Ariell Reshef  
03 Apr 2008 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

03 Apr 2008 jon e

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

25 Aug 2008 Byron Williams  
01 Dec 2009 Nate Yoder  
01 Dec 2009 Nate Yoder

Has issues with resizing

14 Feb 2010 Paul

Hi,

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.
----------------------------------------------------------------------
clf
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'});

set(hAxes,'XTick',[])
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

01 Mar 2010 Jeff ahrens

Nice simple program. Just what I needed.

10 Mar 2011 Rubakumar Sankararaj  
23 May 2011 paolo

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

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
specialized Zhipeng Gao 22 Oct 2008 08:26:41
plotting Zhipeng Gao 22 Oct 2008 08:26:41
plot Zhipeng Gao 22 Oct 2008 08:26:41
plotyy Zhipeng Gao 22 Oct 2008 08:26:41
layer plot Zhipeng Gao 22 Oct 2008 08:26:41
graphics Zhipeng Gao 22 Oct 2008 08:26:41
specialized plot Zhipeng Gao 22 Oct 2008 08:26:41
plot Robert 14 Jun 2010 18:33:02
graphics Mike W. 14 May 2011 11:06:47
layer plot Mike W. 14 May 2011 11:06:55
plotyy Jay 18 Jul 2011 13:05:56
graphics wilson 29 Dec 2011 11:18:04

Contact us at files@mathworks.com