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

조회 수: 3 (최근 30일)
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.

채택된 답변

Matt Cohen
Matt Cohen 2016년 5월 19일
편집: Matt Cohen 2016년 5월 19일
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

추가 답변 (1개)

Ha Sen
Ha Sen 2016년 5월 19일
Hi Matt,
Thank you very much. I will try your modified version.
Best, Hassan

카테고리

Help CenterFile Exchange에서 Two y-axis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by