Hi, I want to plot 2 bar and 1 line in one graph with 3 different scales ( 3 y-axes).

4 views (last 30 days)
I tried plotyy plotyyy and addaxis, hold on, etc. My scales starts from very small to very big so each needs his own y axes. Could you please help.

Accepted Answer

Matt Cohen
Matt Cohen on 19 May 2016
Edited: Matt Cohen on 19 May 2016
Hello,
I understand that you are interested in plotting 2 bar charts and 1 line in a single graph containing 3 distinct y-axes.
The "plotyyy" function can be slightly modified to accomplish something like this. Inside this function, it calls the "plotyy" function to create the first pair of y-axes. The documentation for "plotyy" shows that one of the possible syntaxes accepts inputs 'function1' and 'function2'; as stated in the documentation, these inputs can be either function handles or strings specifying functions "plot", "semilogx", "semilogy", "loglog", "stem", or any MATLAB® function that accepts the syntax "h = function(x,y)".
To plot two bar charts with distinct axes using "plotyy", you can pass in 'bar' as the function name for inputs 'function1' and 'function2'. I have created a modified version of the "plotyyy" function, named "plotyyy_mod", that contains this change in it. I have also added some additional lines of code for formatting the plots to make them slightly more visible. I have attached the "plotyyy_mod.m" file here; I have also included some example code that produces such a figure and a screenshot of the resulting figure:
% Create the data
N = 11;
y1 = [75 91 105 123.5 131 150 179 203 226 249 281.5];
y2 = fliplr(y1)+200;
y3 = linspace(0,100,N);
x1 = linspace(0,N,N);
x2 = linspace(0,N,N);
x3 = linspace(0,N,N);
function1 = 'bar';
function2 = 'bar';
[ax,hlines] = plotyyy_mod(x1,y1,x2,y2,x3,y3,function1,function2);
You can play around with this code more to make the formatting fit your desired outcome.
Please note that "plotyy" is not a recommended function to use anymore. Instead, it is recommended that "yyaxis" be used when trying to create a chart with two axes. However, "plotyy" more easily allows for the addition of a third axes here.
I hope this information proves to be helpful.
Matt

More Answers (1)

Ha Sen
Ha Sen on 19 May 2016
Hi Matt,
Thank you very much. I will try your modified version.
Best, Hassan

Categories

Find more on Two y-axis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!