Multiple line and bar with two y axis.

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 Comments

Do you want all 6 of these in the same plot, or would two plots in the same figure be okay?
I want all 6 of these in the same plot.
What do you mean my "y-axis" exactly?
Sunil Oulkar
Sunil Oulkar on 30 Jan 2017
Edited: Sunil Oulkar on 30 Jan 2017
like this image.
Being one of the less knowledgeable people on this cite, I can only provide you with a janky solution to this - it's simple to do for a few plots, but it would take some time to make a function that automatically does so.
@John: :-) And I would vote for this, when you post it as answer and add the code. yyaxis will be the solution.
Truth be told my above solution involved calling
text(3.02,.1,'10')
multiple times to define the right side axis, and scaling your second values to fit within the plot.
Thanks for stopping me before I pursued such a needless and tedious solution.
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.
Changed my solution to accommodate three plots per axis!

Sign in to comment.

Answers (1)

John Chilleri
John Chilleri on 30 Jan 2017
Edited: John Chilleri on 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 Comments

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.
Changed my solution to accommodate three plots per axis!

Sign in to comment.

Asked:

on 29 Jan 2017

Edited:

on 31 Jan 2017

Community Treasure Hunt

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

Start Hunting!