필터 지우기
필터 지우기

Multiple line and bar with two y axis.

조회 수: 3 (최근 30일)
Sunil Oulkar
Sunil Oulkar 2017년 1월 29일
편집: John Chilleri 2017년 1월 31일
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
Sunil Oulkar
Sunil Oulkar 2017년 1월 31일
I am using Matlab 2013b. plotyy gives only two line. I want 3 bar left hand side and 3 line in right hand side on same plot.
John Chilleri
John Chilleri 2017년 1월 31일
Changed my solution to accommodate three plots per axis!

댓글을 달려면 로그인하십시오.

답변 (1개)

John Chilleri
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
Sunil Oulkar
Sunil Oulkar 2017년 1월 31일
I am using Matlab 2013b. plotyy gives only two line. I want 3 bar left hand side and 3 line in right hand side on same plot.
John Chilleri
John Chilleri 2017년 1월 31일
Changed my solution to accommodate three plots per axis!

댓글을 달려면 로그인하십시오.

카테고리

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