File Exchange

image thumbnail


version (2.15 KB) by Denis Gilbert
PLOTYYY - Extends plotyy to include a third y-axis


Updated 14 Nov 2001

View License

PLOTYYY builds upon plotyy to include a third y-axis. It also provides an easy way of labelling all three y-axes at once.

Cite As

Denis Gilbert (2020). plotyyy (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (73)

thanks for code.. but anyone can help me with printing the figure??

Good function. I have made a couple of changes to suite my requirements.
-Changed the colour of the 3rd plot line/axis to match Matlab colour scheme.
-Removed nargin statement at the beginning and replaced with an exist statement for identifying empty labels.
-Added a datetick input that enables xaxis to be labeled with datetick string.

See code below.
function [ax,hlines] = plotyyy(x1,y1,x2,y2,x3,y3,ylabels,dateticks)
%PLOTYYY - Extends plotyy to include a third y-axis
%Syntax: [ax,hlines] = plotyyy(x1,y1,x2,y2,x3,y3,ylabels)
%Inputs: x1,y1 are the xdata and ydata for the first axes' line
% x2,y2 are the xdata and ydata for the second axes' line
% x3,y3 are the xdata and ydata for the third axes' line
% ylabels is a 3x1 cell array containing the ylabel strings
% dateticks string input for datetick e.g. ('dd/mm/yy' or 'yyyy')
%Outputs: ax - 3x1 double array containing the axes' handles
% hlines - 3x1 double array containing the lines' handles
%y1=x; y2=x.^2; y3=x.^3;
%ylabels{1}='First y-label';
%ylabels{2}='Second y-label';
%ylabels{3}='Third y-label';
%[ax,hlines] = plotyyy(x,y1,x,y2,x,y3,ylabels);
%legend(hlines, 'y = x','y = x^2','y = x^3',2)
%m-files required: none
%Author: Denis Gilbert, Ph.D., physical oceanography
%Maurice Lamontagne Institute
%Dept. of Fisheries and Oceans Canada
%April 2000; Last revision: 14-Nov-2001
if ~exist('ylabels')
%Use empty strings for the ylabels
ylabels{1}=' '; ylabels{2}=' '; ylabels{3}=' ';

%Plot the first two lines with plotyy
[ax,hlines(1),hlines(2)] = plotyy(x1,y1,x2,y2);
%determin dateticks (if applicable)
if exist('dateticks')
cfig = get(gcf,'color');
pos = [0.1 0.1 0.7 0.8];
offset = pos(3)/5.5;
%Reduce width of the two axes generated by plotyy
pos(3) = pos(3) - offset/2;
%Determine the position of the third axes
pos3=[pos(1) pos(2) pos(3)+offset pos(4)];
%Determine the proper x-limits for the third axes
limx3=[limx1(1) limx1(1) + 1.2*(limx1(2)-limx1(1))];
%Bug fix 14 Nov-2001: the 1.2 scale factor in the line above
%was contributed by Mariano Garcia (BorgWarner Morse TEC Inc)
%3rd line colour
lineColor = [0.9290 0.6940 0.1250];
hlines(3) = line(x3,y3,'Color',lineColor,'Parent',ax(3));
%Hide unwanted portion of the x-axis line that lies
%between the end of the second and third axes
line([limx1(2) limx3(2)],[limy3(1) limy3(1)],...
%Label all three y-axes


@Patrick Marquardt.
Is that the only reason you downvoted this function?...
A function can never meet all individual needs. That's why one is able to modify them.
Just Label the x axis the same way as the y axes. Go into the function and add:



@ Neil Stapleton: give the function Matrices instead of vectors. If your vectors are column vectors:
x1 = xline1; y1 = yline1;
x2 = [xline2, xline3]; y2 = [yline2, yline3];
x3 = xline4; y3 = yline4;
[ax,hlines] = plotyyy(x1,y1,x2,y2,x3,y3,ylabels);

In the function you have to modify line 45:
[ax,hlines(1),hlines(2)] = plotyy(x1,y1,x2,y2); --> [ax,hlines(1),hlines(2:3)] = plotyy(x1,y1,x2,y2);
and line 72:
hlines(3) = line(x3,y3,'Color','r','Parent',ax(3)); --> hlines(4) = line(x3,y3,'Color','r','Parent',ax(3));

How do you label the x-axis in that case???

Danyang Li

Kai Luo

Is it possible to plot multiple lines for the same axis? I want to plot one function for the first axis, two functions for the second axis, and one for the third. I have not been able to figure out how to do this.

Felix Lu


How would you still use features like

hold on;


It is a very nice submission, but it doesn't work with datetime values as x. The reason for this is that the xlim property can't be set with datetime values. The annoying workaround is to use datenum instead, and use xtickformat.


It was helpful for me. Thank you


I found these labels are interesting and easy



'XColor','k' -> 'XColor','none'

no longer need hiding unwanted x-axis line.

Great code. Question, how would i go about changing the direction of any of these axes plotted using this function? Thanks.

xue dong


xue dong

when i use it,i can't get the figure...I don't understand hou to use the code.

Hello Thank you for sharing your code. It is really helpful. I tried to modify it to get a logarithmic scale for all x and y axes, but for some reason it works perfectly for the 1st two y axes, but the third one is shifted to the right. Any ideas why or what could be the solution ?

Thank You Mrs. Denis. It was useful for my work.

Shiva Asapu

Lijing Chen


Jinghui Wu

thank you

rainy cao

good,it is useful.


Hi, I have the same problem that fitou below asked:
I am using the function plotyyy and it works fine.
My problem occurs when change the 'xlim' (set(ax,'xlim',[1,12089]); )
The two first curve extend from the first Y axis to the second Y axis
wich is good because this area corresponds with path area. But the third
curve extends from the first Y Axis to the third Y axis wich is outside
of the path area.
As a result my third curve have a part outside of the path area..

Is anybody know how can I solve this ?



Dear Dr. Denis Gilber

this is really a great function, but I have a problem that I want to draw each function with different labels (dash, solid, itc.).

How could I make this? please help me

hi, i have extended your code for plotting 4 variable in the same plot. i want to share the code with everyone.

Hi. Great code! I'm having trouble with data-tips though. I can get data tips to appear on the 2nd and 3rd axes, but not the first. Any ideas? Thanks!

Hey, when I run your example, I'm getting an error that says "Out of memory. The likely cause is an infinite recursion within the program." I would appreciate any help with this.

wenzhu wang

How to zoom in or zoom out the added line?

Wei Li

Why my matlab can't implement this function? The warning says that "Error: Function definitions are not permitted in this context." Is it result from the lower version of matlab?

Marc Timmer

Tino K.

Hey to everyone,

is it possible to have the third plot as a bar? I don't know how to solve the problem.

I would be pleased if somebody can give me an idea.


Hey, thanks for the code! It works well for me!

However, I can't make the visibility off for the figure. Anyone knows?

Works well except when using view(-90 90) to rotate the plot it only rotates the first two and not the last plot?


anyone figure out how to use datetick for x axis with this function?

Hi, if i would to use this function for "axes" of GUI instead of "figure"?

marc Oshe

Hello, thanks for the function plotyyy. I have a problem with x axis. It gives a far too wide area. How can I restrict the xlim such that I can have yearly axis? I tried to change the 'xlim' (set(ax,'xlim',[2001,2013]); ) but I just get error.




Ana C



I am using the function plotyyy and it works fine.
My problem occurs when change the 'xlim' (set(ax,'xlim',[1,12089]); )
The two first curve extend from the first Y axis to the second Y axis
wich is good because this area corresponds with path area. But the third
curve extends from the first Y Axis to the third Y axis wich is outside
of the path area.
As a result my third curve have a part outside of the path area..

Is anybody know how can I solve this ?


Great function! However, I cant seem add an xaxis label. An example would be nice :)


Zooming and data cursor problem .. .
only one layer is zooming . . .
what about other 2 layers . . .
plz help

It appears my first comment failed to reach the page. Basically, if you want to use this in a subplot structure do:
- comment out "figure(..."
- pos=get(gca,'pos'); instead of pos = [0.1 0.1 0.7 0.8];
- pos(3)=pos(3)-offset; instead of pos(3)=pos(3)-offset/2;

correction to the second point:
I changed from
pos = [0.1 0.1 0.7 0.8];
is what I was trying to say.


qiong lin


First thanks for writing such a useful code. I have to add up to three axis and plotyyy works fine.

But the code only generates in new figure, and I didn't manage to generate the three axes into existing figure despite hard trying. Can you please show me how to solve the problem ?

To force the axes to generate in my existing figure, the code
'figure('units','normalized','DefaultAxesXMinorTick','on','DefaultAxesYminorTick','on')' in plotyyy.m is put in comment; plus, position unit converted in centimeter as is the case in my own figure.

The axis 1 and 2 show up, but not the third axis!!

Please any help would be really appreciated.

Wu Zhiyong

Thanks, very useful!

Can the third line be changed to be a bar plot?


Very well done! thank u! :)

Karel Castro

Can't use it calling the file from path, but very useful, thanks! Also labels didn't works as is stated in the example.

Hassan Ri

does not plot two x axes, but plotxx is listed as one of the keywords

YI xu

cannot use in subplot, always generate in a new plot

Izru Garner

Easy to use, and very practical!

Anja Noser

Libor Wilda

looks realy good

Bejoy Mathews

Thanks a lot..
But do you know any which can plot 5 axes at once?

Si Betts

Chitra Parthiban

Thanks. It is very useful to plot and analysis the online data's together

Lina Chang

Thanks for coming up with a way of adding additional axes.

robs goldy

Very good

Olivier G

small and simple but useful

MATLAB Release Compatibility
Created with R12.1
Compatible with any release
Platform Compatibility
Windows macOS Linux