Problem with hold onand plotyy

2 views (last 30 days)
Atteyeh Natanzi
Atteyeh Natanzi on 25 Aug 2015
Edited: dpb on 25 Aug 2015
I am tying to use a plotyy and hold on to plot 4 diagrams in 2 y axes and it did not work, can anybody see where is the problem? it only plot 2 first diagrams
load dexpan2Cp;
x1=dexpan2Cp(:,2);
x1=x1/3600;
y1=((dexpan2Cp(:,3)+dexpan2Cp(:,4))/2000)*32.5;
load Dexpan2_2C;
x2=Dexpan2_2C(:,1);
x2=x2/3600;
y2=Dexpan2_2C(:,2);
A = importdata('Scan Session94-Dexpan19.txt','\t',5);
Dexpan19p=A.data;
save('Dexpan19p.mat','Dexpan19p');
x3=Dexpan19p(:,2);
x3=x3/3600;
y3=((Dexpan19p(:,3)+Dexpan19p(:,4))/2000)*32.5;
load Dexpan19C;
x4=Dexpan19C(:,1);
x4=x4/3600;
y4=Dexpan19C(:,2)+3.3;
x1 = x1(x1<115.7); y1 = y1(1: length(x1) , :);
x2 = x2(x2<115.7); y2 = y2(1: length(x2), :);
x3 = x3(x3<115.7); y3 = y3(1: length(x3), :);
x4 = x4(x4<115.7); y4 = y4(1: length(x4), :);
hold off;
figure;
[ax, h1, h2] = plotyy(x1,y1,x2,y2);
set(h1,'g',y1,'DisplayName','1');
set(h2,'b',y2,'DisplayName','2e');
hold(ax(1),'on');
plot(ax(1),x3,y3,'r',y3,'DisplayName','3');
hold(ax(2),'on');
plot(ax(2),x4,y4,'k',y4,'DisplayName','4');
xlabel('Time (h)');
ylabel(ax(1),' Pressure (MPa)');
ylabel(ax(2),'Tempereture (°C)');
legend('show');
result

Answers (1)

dpb
dpb on 25 Aug 2015
set(h1,'g',y1,'DisplayName','1');
is a syntax error, the handle object returned in h1 from plotyy is (possibly an array of) line handle(s) to the lines on the LH axes (and similarly for RH axes in second). The set command syntax to set the color for the line associated with h1 would be
set(h1,'color','g','DisplayName','1');
as it has no way to associate the variable with the handle and the property name is 'color', not a list of default colors as would be required for the above to work.
plot(ax(1),x3,y3,'r',y3,'DisplayName','3');
Similarly, there's a syntax in the follow-on plot command above; the triplet form is plot(x,y,linespec,...); again there's no connection to the variable excepting by position. Use
plot(ax(1),x3,y3,'r','DisplayName','3');
instead. It would be good practice to return a line handle to these additional lines by augmenting the two handle variables into arrays so you have access to them directly for further manipulation if needed, but not mandatory. Also, if the lengths of the timeseries are the same you can place the y-values to go on each axes in an array by column and plot in one call to plotyy() instead of multiple...
[hAx,hL1,hL2]=plotyy([x1 x3],[y1 y3],[x2 x4],[y2 y4]);
where the arrays are assumed to be the same length and column vectors, fixup as necessary if not but just showing possible alternate and perhaps simpler syntax...
  4 Comments
Atteyeh Natanzi
Atteyeh Natanzi on 25 Aug 2015
I did the changes and can not run it. Here is my data. I would really appreciate if you could help.
dpb
dpb on 25 Aug 2015
Edited: dpb on 25 Aug 2015
Your .mat files don't match up with the script...some have x,y vectors, some still have the Dex*** variables. You need to go back to the beginning and work through with a consistent set of data first, then make the changes I noted earlier in the syntax of the plot commands.
>> whos -file dexpan19c
Name Size Bytes Class Attributes
Dexpan19C 1371168x2 21938688 double
>> whos -file dexpan19p
Name Size Bytes Class Attributes
x3 500000x1 4000000 double
y3 500000x1 4000000 double
>> whos -file dexpan2_2C
Name Size Bytes Class Attributes
Dexpan2_2C 416695x2 6667120 double
>> whos -file dexpan2Cp
Name Size Bytes Class Attributes
x1 500000x1 4000000 double
y1 500000x1 4000000 double
>>
Not knowing then who's been scaled and how, difficult to make realistic plot. But, the script won't run when try to use the wrong variable name after load for the files that aren't consistent first, then the issue of scaling or not having been done consistently, etc., ...

Sign in to comment.

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!