File Exchange

image thumbnail

Plot with multiple Y-axes

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

41 Downloads

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 (https://www.mathworks.com/matlabcentral/fileexchange/67349-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

xall=linspace(1991,2018,28);
y1=[282.28,302.56,357.83,430.13,374.63,394.93,526.64,628.11,737.87,983.56,1290.78,1532.00,1788.45,1894.04,2102.64,2469.34,2569.48,3033.13,3819.39,4212.20,5046.60,6067.45,6516.38,7312.41,8104.23,9134.89,9929.47,11108.69];
y2=[1236.19,1481.41,1591.49,1561.66,1665.70,1652.83,2436.74,2354.16,3285.56,2711.22,3185.90,4718.71,5393.77,5869.60,6436.39,7116.77,7869.97,8872.50,12564.96,12932.25,13193.52,14614.38,15793.13,16146.46,17645.37,17889.13,18831.94,21434.90
];
y3=[3006.03,3173.90,3404.71,3256.11,3324.45,4008.41,4273.66,5397.17,5565.26,6670.83,6942.24,7481.52,8213.58,8235.73,10238.62,11047.14,13273.94,14624.62,18057.51,21297.24,22016.30,26373.50,29935.53,31254.34,33409.86,34665.83,35293.60,38489.56
];

ResFund=plotNy(xall,xall,xall,{y1,y2,y3},[],...
'XAxisLabel','Years',...
'YAxisLabels',{'Govt. Funding - Basic Res.' 'Govt. Funding - Applied Res.''Govt. Funding - Experimental Dev.'},...
'TitleStr','Research Funding',...
'Grid','on',...
'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

patpin

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?

vanrapa

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 '9.3.0.713579 (R2017b)'.

monica

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

Tom

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.

Darshan

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.

Frank

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

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

Inspired: plotNAx

Community Treasure Hunt

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

Start Hunting!