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
採用された回答
その他の回答 (1 件)
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
2016 年 1 月 22 日
dpb
2016 年 1 月 22 日
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
2016 年 1 月 22 日
dpb
2016 年 1 月 23 日
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 Exchange で Two y-axis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

