Multiple line and bar with two y axis.

4 Ansichten (letzte 30 Tage)
Sunil Oulkar
Sunil Oulkar am 29 Jan. 2017
Bearbeitet: John Chilleri am 31 Jan. 2017
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 Kommentare
Sunil Oulkar
Sunil Oulkar am 31 Jan. 2017
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 am 31 Jan. 2017
Changed my solution to accommodate three plots per axis!

Melden Sie sich an, um zu kommentieren.

Antworten (1)

John Chilleri
John Chilleri am 30 Jan. 2017
Bearbeitet: John Chilleri am 31 Jan. 2017
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 Kommentare
Sunil Oulkar
Sunil Oulkar am 31 Jan. 2017
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 am 31 Jan. 2017
Changed my solution to accommodate three plots per axis!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Two y-axis finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by