Hello! I am trying to use plotyy in a for loop as in the example code below but the points related to the y achses on the right hand side are not being plotted correctly. I looked but I could not find a solution to this answer. Thank you!

2 views (last 30 days)
x=[1:1:20];
y1=x.^2;
y2=x*2;
cc=linspecer(length(x));
figure(1)
[ax,p1,p2]=plotyy(x,y1,x,y2); % this continuous plot works
hold all
for n=1:length(x)
[ax,p1,p2]=plotyy(x(n),y1(n),x(n),y2(n));
% But if I plot each point separately because I want to plot them in a
% different color, y2 point show up incorrect
p1.Marker = '+';
p2.Marker = '+';
p1.Color=cc(n,:);
p2.Color=cc(n,:);
p1.MarkerSize=15;
p2.MarkerSize=15;
end
hold on

Accepted Answer

dpb
dpb on 23 Jan 2016
Edited: dpb on 26 Jan 2016
OK, dawned on me overnight...use scatter instead. Couple alternatives, probably simplest is
% build handle to scatter using marker and color for plotyy
hSc=@(x,y) scatter(x,y,'SizeData',50,'CData',cc,'Marker','+');
[hAx,hL,hR]=plotyy(x,y1,x,y2,hSc,hSc) % call plotyy with this handle for the two data sets
Modify the customized function to change constant characteristics desired or include other variables (such as 'SizeData', maybe as a named parameter). Of course, plotyy can only pass the x,y values so the others have to be either embedded in a new function handle if use anonymous function or must write your own wrapper function that then calls plotyy with only the two allowed arguments.
Once having plotted, hL, hR will be the scatter objects' handles and can modify their properties as desired.
Of course, you then can use plotyy in a conventional manner to draw the solid lines.
set(hAx,'nextplot','add') % Equivalent HOLD ON for multiple axes
plotyy(hAx,x,y1,x,y2) % add lines onto existing axes markers
ADDENDUM
Example--
cc=round(linspace(1,length(colormap),length(x))); % spread over colormap by length of data
sz=100; % set a size parameter for marker
hSc=@(x,y) scatter(x,y,'SizeData',sz,'CData',cc,'Marker','+'); % embed in anon func
[hAx,hS1,hS2]=plotyy(x,y1,x,y2,hSc,hSc); % draw markers with handle
set(hAx,'nextplot','add') % "hold on" for multiple axes
[~,hL1,hL2]=plotyy(hAx,x,y1,x,y2) % add lines over marker
results in
  4 Comments
dpb
dpb on 27 Jan 2016
Glad to help...sometimes these little "issues" take a little while festering before the right answer comes along. I'm a little puzzled over the plotyy behavior of duplicating axes handles so profusely; I'd have thought similar behavior to subplot wherein it recognizes the identical position coordinates and reuses an existing axis instead. Particularly, since seemed to do so for RH axis but not for LH. I'm presuming a similar behavior was observed in later release than R2012b that is latest I have installed? If so, a demo as above (albeit only need 3 or 4, not 20) might be worth a query to TMW as to whether is intended behavior or not...I'm just not certain of actual intent (or if there is one defined).

Sign in to comment.

More Answers (1)

dpb
dpb on 22 Jan 2016
Edited: dpb on 22 Jan 2016
I'd guess probably because hold only works on the current axes which will be ax(1). Try
hold(ax(1),'on')
hold(ax(2),'on')
after the first plotyy call in the loop. Likely you also don't want/need that outside the loop.
  4 Comments
DE
DE on 22 Jan 2016
linespecer is just to set distinguishable colors, you can find the file here. size of cc is 20 3, it is a color triplet for each marker. I don't get errors in the issue described above. It is just not plotting the second markers properly. The plot below is what I'm getting. The markers for y1 are following the plot (blue curve) but the markers from y2 in the for loop seem to just be on a straight line even though they should follow the red line. Thank you for your effort!
dpb
dpb on 23 Jan 2016
Ok, it's a function rather than an array, then.
Well, when I try plotyy in a loop here, I get a new axis handle for every invocation for the LH axis whereas it seems the right one is saved. Following the loop, inquiring for the children of figure results in--
>> get(get(gcf,'children'),'type')
ans =
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
'axes'
>> length(ans)
ans =
21
>>
Note there are 21 axes for the n=20 points. What's happening is since there's only a single point on each axis, the auto-ranging is setting each of those axes limits to unit value and scaling such that each is at the midpoint. I couldn't get the straight line here w/ R2012b, even, I get all the points on the origin as well as the markers.
It's late now, I don't have an otomh solution; I think perhaps the answer would be to use plotyy only once to create the two axes and then plot into those specific axes with the calling sequence using the specific axis handle for the LH, RH axis, specifically. Maybe in the morning I'll have a chance to 'spearmint a little.

Sign in to comment.

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!