Hi, I want to plot 2 bar and 1 line in one graph with 3 different scales ( 3 y-axes).
5 次查看(过去 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.
0 个评论
采纳的回答
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
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Two y-axis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!