Plot two Y-Axis, multiple lines on the second from line-fitted points and call out only the fitted lines in the legend

2 views (last 30 days)
I have several data sets that share an X-Axis. X is %, Y1 is temp and Y2 is gas production. I have two temp sets and 6 gas sets. I cannot use plotyy because these are points. If I use the fit command, I get red lines that I have to manually change color (hard to do if there are on top of each other). I created a second y axis but now I cannot control the legend. I am getting all points and lines and I cannot turn on or off any of them. I've tried [p1 p2], '----', '----'); and that won't work. Here is what I have that kind of works:
A = importdata('Cheetah_Al.xlsx');
AX = A.data(1:10,1);
AT = A.data(1:10,2);
AF = A.data(1:10,3);
AF3 = A.data(1:10,4);
AG = A.data(1:10,5);
RX = A.data(14:23,1);
RT = A.data(14:23,2);
RF = A.data(14:23,3);
RF3 = A.data(14:23,4);
RG = A.data(14:23,5);
f1 = fit(AX,AT,'line');
f2 = fit(AX,AF,'line');
f3 = fit(AX,AF3,'line');
f4 = fit(AX,AG,'line');
f5 = fit(RX,RT,'line');
f6 = fit(RX,RF,'line');
f7 = fit(RX,RF3,'line');
f8 = fit(RX,RG,'line');
p1 = plot(f1,AX,AT,'r*');
hold on;
p5 = plot(f5,RX,RT,'b*');
ax1 = gca;
set(ax1,'XColor','k','YColor','k');
xlabel('% wt. Aluminum');
ylabel('Temperature (K)');
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','bottom',...
'YAxisLocation','right','Color','none','XColor','k','YColor','k');
gca = ax2;
hold on;
p2 = plot(f2,AX,AF,'k*');
hold on;
p3 = plot(f3,AX,AF3,'g*');
p4 = plot(f4,AX,AG,'b*');
p6 = plot(f6,RX,RF,'c*');
p7 = plot(f7,RX,RF3,'m*');
p8 = plot(f8,RX,RG,'y*');
xlabel('% wt. Aluminum');
ylabel('Gas Production (mol/kg)');

Answers (1)

dpb
dpb on 27 Jun 2015
Edited: dpb on 27 Jun 2015
"I cannot use plotyy because these are points."
Of course you can...
[hA,hL,hR]=plotyy(x1,y1,x2,y2,@plot,@scatter);
Place the x- and y- axes variables for each axis in an array by column instead of dealing with each individually. Even better would be to use arrays rather than individual variable names from the git-go; simplifies much in Matlab coding.
A more precise definition of what you expect the resulting plot to look like and attaching the data set (as a .mat file would one good way) for others to be able to "play" would lead to more direct solutions to specific issues undoubtedly.
ADDENDUM
On the query re: legend -- use the two arrays of handles to the left/right axes objects (lines, in particular) returned from plotyy in the call to legend to label those specific lines desired. Build an array of titles desired corresponding to the specific lines (handles) you wish to appear in the legend and then pass that with the vector of line handles in one:one correspondence.

Categories

Find more on Two y-axis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!