Legends display incorrect markers

7 ビュー (過去 30 日間)
Ryan
Ryan 2012 年 11 月 8 日
The Incorrect markers will appear next to the strings in the legend.
for k = 1:size(Scattering,1)
hold on
if grade(k) == 0
figure(1)
plot(XS(k,x),XS(k,y),'bo')
elseif grade(k) >= 1
figure(1)
plot(XS(k,x),XS(k,y),'ro')
end
if grade(k) == 13 || grade(k) == 14
figure(2)
plot(XS(k,x),XS(k,y),'rx')
elseif grade(k) >= 16
figure(2)
plot(XS(k,x),XS(k,y),'kx')
end
end
set(get(get(figure(1),'children'),'xLabel'),'string',sprintf('PC%d',x))
set(get(get(figure(2),'children'),'xLabel'),'string',sprintf('PC%d',x))
set(get(get(figure(1),'children'),'yLabel'),'string',sprintf('PC%d',y))
set(get(get(figure(2),'children'),'yLabel'),'string',sprintf('PC%d',y))
set(get(get(figure(1),'children'),'title'),'string','Benign vs. Malignant')
set(get(get(figure(2),'children'),'title'),'string','High grade vs. Low grade cancer')
%get(get(figure(2),'children'),'children')
%legends correspond to plot order and not to given plots
legend(get(get(figure(1),'children'),'children'),'Benign','Malignant','Location','NorthEastOutside')
legend(get(get(figure(2),'children'),'children'),'High Grade','Low Grade','Location','NorthEastOutside')
hold off
I attempted to create a legend with a blue circle next benign and a red circle next to malignant. Instead there are blue circles next to both, and not coincidentally the last two objects plotted on the axes were both blue circles.
  3 件のコメント
Ryan
Ryan 2012 年 11 月 8 日
In the most recent run, Scattering was a 150x41 matrix, grade was a 150x1 matrix, and XS was a 150x5 matrix. I forgot to mention this warning pops up.
Warning: Ignoring extra legend entries.
> In legend at 291
In PLSAnalysis_Scattering at 45
Warning: Ignoring extra legend entries.
> In legend at 291
In PLSAnalysis_Scattering at 46
Ryan
Ryan 2012 年 11 月 8 日
And x=1 y=2...oops

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

採用された回答

Matt Fig
Matt Fig 2012 年 11 月 8 日
編集済み: Matt Fig 2012 年 11 月 8 日
I would dispense with keeping track of all that stuff anyway. Switching current figures in a loop, keeping track of handles, calling great-grandparents and children, setting the string property of xlabel and title objects and all? Not here!
This is much more straightforward and easier to understand:
figure
idx = grade==0;
plot(XS(idx,1),XS(idx,2),'bo')
hold on
idx = grade>=1;
plot(XS(idx,1),XS(idx,2),'ro')
xlabel(sprintf('PC%d',x))
ylabel(sprintf('PC%d',y))
title('Benign vs. Malignant')
legend('Benign','Malignant','Location','NorthEastOutside')
figure
idx = grade==13 | grade==14;
plot(XS(idx,1),XS(idx,2),'rx')
hold on
idx = grade>=16;
plot(XS(idx,1),XS(idx,2),'kx')
xlabel(sprintf('PC%d',x))
ylabel(sprintf('PC%d',y))
title('High grade vs. Low grade cancer')
legend('High Grade','Low Grade','Location','NorthEastOutside')

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2012 年 11 月 8 日
Apparently the children order being returned is not what you expect. I would recommend explicitly saving the handles rather than getting children's grandchildren's great grandparents!
  2 件のコメント
Ryan
Ryan 2012 年 11 月 8 日
Had a lot of trouble with this method. I still couldn't get the second legend to display correctly, and I have no idea why I needed two hold on's.
hold on
fig1hand = figure(1);
fig2hand = figure(2);
for k = 1:size(Scattering,1)
hold on
if grade(k) == 0
plotbenhand = plot(get(fig1hand,'children'),XS(k,x),XS(k,y),'bo');
elseif grade(k) >= 1
plotmalhand = plot(get(fig1hand,'children'),XS(k,x),XS(k,y),'ro');
end
if grade(k) == 13 || grade(k) == 14
plot(get(fig2hand,'children'),XS(k,x),XS(k,y),'rx')
elseif grade(k) >= 16
plot(get(fig2hand,'children'),XS(k,x),XS(k,y),'kx')
end
end
hold off
legend(get(fig1hand,'children'),['mal';'ben'])
legend(get(fig2hand,'children'),['hig';'low'])
Sean de Wolski
Sean de Wolski 2012 年 11 月 9 日
You need the second hold on because you create the figures after the call to the first one. Thus they don't see the first one.
However, I don't see why you need either, considering you're only plotting to each figure once...

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

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by