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!

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

 採用された回答

dpb
dpb 2016 年 1 月 23 日
編集済み: dpb 2016 年 1 月 26 日
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 件のコメント

DE
DE 2016 年 1 月 25 日
編集済み: DE 2016 年 1 月 25 日
Thank you very much for your help! There seems to be an error with using the color (cc) in the scatter function, I also tried
hSc=@(x,y) scatter(x,y,'color',cc,'+');
But apparently there is no 'color' option in the scatter function. Without the cc your suggestion works, i.e. I have the lines and the crosses on top, but then all the markers have the same color. You mentioned you might have other ideas how to solve this? I really appreciate your hep so far! I never got too much into plotting with matlab am still a bit lost using it.
dpb
dpb 2016 年 1 月 25 日
編集済み: dpb 2016 年 1 月 25 日
Blew it writing the anonymous function handle--got the unnamed arguments in wrong order prior to using a named one, sorry. See updated Answer for correction.
DE
DE 2016 年 1 月 26 日
Thank you for the example!!! Now I can also do it right ;-)
dpb
dpb 2016 年 1 月 27 日
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).

サインインしてコメントする。

その他の回答 (1 件)

dpb
dpb 2016 年 1 月 22 日
編集済み: dpb 2016 年 1 月 22 日
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 件のコメント

DE
DE 2016 年 1 月 22 日
Thank you for your answer! I tried it but it doesn't solve the problem.
What is linspecer?
Oh, and whatever it is,
cc=linspecer(length(x));
will return just a single value so I'd think the terms
p1.Color=cc(n,:);
would crash on a bounds error.
What is
size(cc)
after the assignment, just for grins?
DE
DE 2016 年 1 月 22 日
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!
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeTwo y-axis についてさらに検索

質問済み:

DE
2016 年 1 月 22 日

コメント済み:

dpb
2016 年 1 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by