File Exchange

image thumbnail

Plot with multiple Y-axes

version (9.61 KB) by Tom R
Plotting function that allows for N y-axes using a common x axis


Updated 11 Aug 2019

View Version History

View License

This function allows for results to be plotted on N separate y-axes. These y-axes share a common x axis, but do not need to share exactly the same x/y data or size. The function supports all utilities on the Figure toolbar, including the data tip, zoom/pan, and data brush. The plot scales upon resize of the figure, maximizing the utilization of the figure's space. The function supports many of the native property commands and syntax for 'plot' such as 'LineStyle','MarkerStyle',etc.. Additionally, saving and loading as .fig is supported, as well as all other MATLAB supported image types.

Cite As

Tom R (2021). Plot with multiple Y-axes (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (31)

Qin Huang

Muhammad Ramadhani

never mind my last question, already solved it thx man...

Muhammad Ramadhani

Hi Tom, thx for the code
I tried writing these lines and it's not working, perhaps you can help me? thx


'YAxisLabels',{'Govt. Funding - Basic Res.' 'Govt. Funding - Applied Res.''Govt. Funding - Experimental Dev.'},...
'TitleStr','Research Funding',...
'Xlim',[1991 2018],...
'Ylim',[0 12000; 0 22000; 0 39000]);

Christopher Lamb

Thank you Tom R! This function is an amazing addition.

Konstantin Steinbach

Marita Spörl

Hello Tom,
is there a possibility to put some of the axis on the right side of the plot so that not all y-axis appear on the left side?
thank you anyway, the function is very helpful!

Michael Papst

Peng Peng

Could you specify how to set the bounds for the axis?
I use 'Xlim',[0 100]
It doesn't work.
Thank you!

Leopoldo Brasil

Christopher Lamb


Hello Tom, When I run the sample I get a error:
warning: set: allowing Unit to match axes property units
warning: called from
axes at line 100 column 8
plotNy at line 418 column 12
testPlotNy at line 28 column 4
error: value on right hand side of assignment is undefined
error: called from
plotNy at line 493 column 18
testPlotNy at line 28 column 4

Any idea? (I had to put yr samplecode in a function to run it in Octave GNU 5.2.2 or did I something wrong?

Trevor S

Hey Tom - does this function have the capability to do log scale axes? If not are there a couple lines of code that could be added to include this capability?

Tom R

The plotter can be used to generate subfigures within a bigger application by using a uipanel. This can be done by creating a uipanel in the GUI and passing the figure handle as the 'Parent' argument to the plotter.

Kerry Leith

Hi Tom, can you clarify whether it's possible to use this script to generate a subplot within a figure?


Clovis Bonavides

Thanks! Proved useful to me.

Tom R

Thanks again for more positive feedback! More answers to questions below:
-Unfortunately the nature of the function is not well suited for plottools. However, since all relevant handles of the plot are returned, any changes can be made via set()/get() operations. If you have specific questions feel free to reach out, or search for similar questions applied to simpler figures.
-These handles could also be used to edit the interpreter of the Axis labels. I checked with a large number (>10) signals on >8 axes and had no issue with Latex legend labels, so my suspicion is that there is an issue in the input string or your default interpreter.

Chaitanya Panuganti

Perfect! Just what I was looking for. The syntax blends seamlessly into MATLAB's default syntax and the provided example is very straightforward. Good comments/documentation in the code as well.

Erik Hillberg

Hi Tom. Love this addon. I have however recently got a bug when i call "plottools on". When i do this the Y-axis is outside the visible window and the plots is somewhat zoomed in.

I recently got a new computer and had to install every thing from scratch so not sure if this problem is due to your uppdate or something matlab has changed. I have verison ' (R2017b)'.


Arun Joe Joseph

Arun Joe Joseph

Thanks, Tom for this useful script. Mostly, It seems pretty straightforward. But, I seem to have trouble displaying the legend signal names when displaying more than 8 signals / axes at the moment. It reverts back to default legend text for more than 8 signals. Is there some limit on this, or am I doing something wrong? Also, is there a way to set interpreter to none for Axis labels (similar to what we do for legends), so that it does not for e.g. interpret the characters followed by underscores as subscript?

Tom R

Thank you for your feedback. I have some replies for those facing issues:

-Legend can be disabled by setting 'LegendLocation' to 'none'
-Scatter plots can be made by setting 'LineStyle' to 'none' and 'MarkerStyle' to '.'
-The program has been corrected to support 'outside' legend location
-The example script has be edited to demonstrate how to change the axis color


Farhad Gharagheizi

The worst program I have ever seen!
It forces legend and there is no option to disable it!

Bernardo Castaldo

Thank you for the script, but the legend location is not working well.
Try to set 'Location','northeastoutside'.

Sashank Pappu

Thank you for the example. could anyone here guide me to change the color of the axis?

its hard to get any help from the example given.
i am using Mat lab 2013b. And when i run the indented script its showing error.


How to deal with scatter plots ?

Wesley Neill

I can't agree that the example was clear, or simple. addaxis seems to work the best, though I very much dislike that you cannot specify a parameter to put all of the axis on one side or the other.


Very easy to use, clear example.

Leonhard Rauh

hello Tom,

can you, or someone else post a short example, because i am a rookie with matlab and i have a Problem to run the function.

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

Inspired: plotNAx

Community Treasure Hunt

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

Start Hunting!