Multiple line and bar with two y axis.
1 次查看(过去 30 天)
显示 更早的评论
Hi, I want to plot 3 line with same scale in one y axis and 3 bar having same scale another y axis. Is there a way to do this.
9 个评论
回答(1 个)
John Chilleri
2017-1-30
编辑:John Chilleri
2017-1-31
Hello,
Stealing entirely from Jan Simon's comment, you can use the yyaxis command. The examples in the documentation are very clear and concise.
In summary,
yyaxis left
% Plot your values that you wish to correspond to the left hand side axis
yyaxis right
% Plot your values that you wish to correspond to the right hand side axis
As an additional note, yyaxis was released with R2016a I believe, and if you are not running with that or a newer version, you can use the plotyy command, although it is not recommended.
plotyy works as follows,
plotyy(X1,Y1,X2,Y2)
where the X1, Y1 represents your values associated to the left side y-axis, and the X2, Y2 represents your values associated to the right side y-axis.
If you want to plot three lines per axis using plotyy, you can do the following:
Col1 = [0 .447 .741];
Col2 = [.85 .325 .098];
[AX,H1,H2] = plotyy(X1,Y1,X4,Y4);
AX(1).YColor = Col1;
AX(2).YColor = Col2;
H1.Color = Col1;
H2.Color = Col2;
hold on
[AX2,H1,H2] = plotyy(X2,Y2,X5,Y5);
AX2(1).YColor = Col1;
AX2(2).YColor = Col2;
H1.Color = Col1;
H2.Color = Col2;
%AX2(1).YLim = AX(1).YLim;
%AX2(1).YTick = AX(1).YTick;
%AX2(2).YLim = AX(2).YLim;
%AX2(2).YTick = AX(2).YTick;
[AX2,H1,H2] = plotyy(X3,Y3,X6,Y6);
AX2(1).YColor = Col1;
AX2(2).YColor = Col2;
H1.Color = Col1;
H2.Color = Col2;
%AX2(1).YLim = AX(1).YLim;
%AX2(1).YTick = AX(1).YTick;
%AX2(2).YLim = AX(2).YLim;
%AX2(2).YTick = AX(2).YTick;
where (X1,Y1),(X2,Y2),(X3,Y3) will be aligned with the y-axis on the left, and (X4,Y4),(X5,Y5),(X6,Y6) will correspond to the the y-axis on the right. So before pasting this code, make sure you have your X1-X6 and Y1-Y6 stored and it will do the rest itself!
I realize some of the above lines are unnecessary, but I'm leaving them in so it's clear what the idea is - also, you may encounter the problem of your axis having overlapping numbers - if your numbers are ranged similarly this won't happen, but if it does, it can be solved by setting the axis properties to match each other:
AX(1).YLim = [min max];
AX(1).YTick = [tick locations]; % ex: [0 5 10];
AX(2).YLim = [min2 max];
AX(2).YTick = [tick locations2]; % ex: [100 200 300];
You can also uncomment the lines that are commented out in the longer code and it might do as you wish if your first plots' axis are good for all plots.
Hope this helps!
2 个评论
另请参阅
类别
在 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!