how to plot more than 2 data sets onto one figure with two y-axes and one x-axis, with multiple datasets plotted with respect to the second y-axes and multiple datasets with respect to the first y-axis

Hello,
I'm trying to plot multiple lines (total of 12 data sets) onto one Figure with two y-axes and one x-axis. So far, I have multiple lines onto one Figure, but with one y-axis and one x-axis as shown below. I would like the group of lines at the bottom of the plot (total of 6 data sets) to be plotted (in scale with) or with respect to the second y-axis (not shown in image).
The farthest I've gotten was using the plotyy function, and having one data set plotted in scale with (or with respect to) the second y-axis, but the rest of the lines (11 data sets) plotted in scale with (or with respect to) the first y-axis...does that make sense? Any help would be greatly appreciated.
I initially imported a .txt data file and filtered the data to op_data and non_op_data if you're wondering. I'm using MATLAB R2015a btw. I hope this makes enough sense.
Thanks
RPM = TestCase1.data(:,2);
[row] = find(RPM > 150);
[row_non_op] = find(RPM <= 150); %non_op_row
op_data = TestCase1.data(row,:); %op_data
non_op_data = TestCase1.data(row_non_op,:); %non_op_data
d = TestCase1.textdata(2:end,1); %textdata of timestamp
date = datetime(d,'InputFormat','M/d/y h:mm:ss a'); %Converting textdata into a datetime array
op_date = date(row,1); %op_date
non_op_date = date(row_non_op,1); %non_op_date
non_op_data(:,4:9) = NaN;
if num_cyl == 6
PP_CYL_op = op_data(:,4:9);
SDoPP_CYL_op = op_data(:,10:15);
figure (); hold on
for i = 1:num_cyl
plot(op_date,PP_CYL_op(:,i))
end
hold on
for i = 1:num_cyl
plot(op_date,SDoPP_CYL_op(:,i))
end
end

 Accepted Answer

In the plotyy documentation, see: Use Right y-Axis for Two Data Sets. That may be what you want.

5 Comments

Thanks for your quick response! I have only seen examples of 2 y-axes plots with two data sets, or 3-4 y-axes plots with 3-4 data sets, respectively. Ideally, I would prefer having only 2 y-axes with more than 2 data sets being plotted in scale with (or with respect to) either the first or second y-axis.
I actually have 12 data sets that I would like to plot onto one Figure. 6 data sets plotted with respect to (or in scale with) the first y-axis. The other 6 data sets I would like to have them plotted with respect to (or in scale with) the second y-axis.
I've only gotten as far as plotting 11 data sets with respect to the first y-axis, and 1 data set with respect to the second y-axis.
I've been trying to figure this out for hours...Any ideas or suggestions would be appreciated.
I can't really decipher what you're saying you want, but you can have as many datasets (lines) on either axes in plotyy as desired and they can be associated with either axis at will.
The simplest thing to do is to place all data for the LH axis in one x,y array and those for the RH axis in another pair of arrays arranged by column for each variable. If the x-values are the same for all y in each then it can simply be a vector; if not make both of each pair the commensurate size. If there's a difference in length, augment the shorter with NaN to the longest length; the graphics routines will ignore those and do what is wanted automagically.
Once you've got the arrays, then the call is simply
hA=plotyy(x1,y1,x2,y2); % put x1,y1 on LH, x2,y2 on RH axes
hA will have the axes handles for the two axes; you can then modify them as desired and even add additional plots to either...
hold(hA(1),'on') % set hold for LH axes
plot(hA(1),xnew,ynew) % add another line to the LH axes
If that isn't what you're looking for, try again to explain which datasets would go where and what the axes limits, etc., would be.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 21 Jul 2015

Edited:

dpb
on 22 Jul 2015

Community Treasure Hunt

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

Start Hunting!