i have
Z1={{rand(1,3)},[{rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)}],[{rand(1,3)} {rand(1,3)} ] }
cl='gbrcmykw';
for k4=1:numel(Z1)
y=cell2mat(Z1{k4}');
plot3(y(:,1),y(:,2),y(:,3),[ cl(k4) 's'],'MarkerSize',12,'DisplayName',sprintf('Zhluk %k4',y))
hold on
end
title('Priebeh zhlukovania','FontSize',16,'Color','k')
legend('show','Location','NorthEastOutside')
hold on
and i have
TAZ4=[ 1 2 7; 1 2 8; 1 2 3; 7 4 1; 4 5 6 ]
for k4=1:size(TAZ4,1)
plot3(TAZ4(k4,1),TAZ4(k4,2),TAZ4(k4,3),[cl(k4)'X'],'MarkerSize',10,'DisplayName',sprintf('Tazisko %k4',TAZ))
hold on
end
legend('show','Location','SouthEastOutside')
How do I add two legends to a single plot ?
Thanks.

 採用された回答

Kelly Kearney
Kelly Kearney 2014 年 1 月 27 日

0 投票

I'm not really sure that code does what you think it does... you're looping over the points too many times in your drawT function (I don't think you want that outer loop, just a single i to use if your if cases. Also, %k isn't a valid sprintf format, so you're labels are all the same.
Anyway, here's a simplified version... you can build from there.
MON = [2.8 3.6 17.2; 5.4 8.3 15.8; 2.5 3.2 17.6];
Z1={{rand(1,3)},[{rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)}],[{rand(1,3)} {rand(1,3)} ] }
cl='grbckmyw';
axes('position', [0.1 0.1 0.7 0.8]);
view(3);
hold on;
for ii = 1:size(MON,1)
h1(ii) = plot3(MON(ii,1), MON(ii,2), MON(ii,3), ...
'marker', 'X', ...
'color', cl(ii), ...
'displayname', sprintf('Tazisko %d', ii), ...
'MarkerSize', 10, ...
'linestyle', 'none');
end
cl='gbrcmykw';
for ii = 1:size(Z1,2)
xyz = cat(1, Z1{ii}{:});
h2(ii) = plot3(xyz(:,1), xyz(:,2), xyz(:,3), ...
'marker', 's', ...
'color', cl(ii), ...
'displayname', sprintf('Zhluk %d', ii), ...
'markersize', 10, ...
'linestyle', 'none');
end
leg1 = legendflex(h1, get(h1, 'DisplayName'), 'anchor', {'ne','nw'}, 'buffer', [5 0]);
leg2 = legendflex(h2, get(h2, 'DisplayName'), 'anchor', {'se','sw'}, 'buffer', [5 0]);

1 件のコメント

Tomas
Tomas 2014 年 1 月 27 日
thank you very much for help

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

その他の回答 (1 件)

Kelly Kearney
Kelly Kearney 2014 年 1 月 27 日

1 投票

You can't have two legends associated with a single axis using Matlab's legend. However, you can with legendflex. Remove your legend calls and add this code to the end:
pos = get(gca, 'position');
set(gca, 'position', pos.*[1 1 0.8 1]);
h1 = findall(gca, 'marker', 's');
h2 = findall(gca, 'marker', 'X');
leg1 = legendflex(h1, get(h1, 'DisplayName'), 'anchor', {'ne','nw'}, 'buffer', [5 0]);
leg2 = legendflex(h2, get(h2, 'DisplayName'), 'anchor', {'se','sw'}, 'buffer', [5 0]);
You could make all this plotting a lot cleaner if you saved the handles of your plot objects, and then set things like marker and color after the fact, but I've left that alone for now. Note that I added the resizing of the axis, since unlike legend, legendflex doesn't automatically resize axes to allow space for legends.

6 件のコメント

Tomas
Tomas 2014 年 1 月 27 日
編集済み: Walter Roberson 2014 年 1 月 27 日
I do not know where I have to add, can you help me ?
my code:
my
function drawZ(Z1)
cl='gbrcmykw';
for k4=1:numel(Z1)
y=cell2mat(Z1{k4}');
plot3(y(:,1),y(:,2),y(:,3)[cl(k4)'s'],'DisplayName',sprintf('Zhluk %k4',y))
hold on
end
title('Priebeh zhlukovania','FontSize',16,'Color','k')
legend('show','Location','NorthEastOutside')
function drawT(TAZ)
cl='grbckmyw'
for k4=1:size(TAZ4,1)
plot3(TAZ4(k4,1),TAZ4(k4,2),TAZ4(k4,3[cl(k4)'X'],'DisplayName',sprintf('TTT %k4',TAZ4))
hold on
end
title('Priebeh zhlukovania','FontSize',16,'Color','k')
legend('show','Location','SouthEastOutside')
command window:
Z1={{rand(1,3)},[{rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)}],[{rand(1,3)} {rand(1,3)} ] }
drawZ(Z1)
hold on
TAZ4=[ 1 2 7; 1 2 8; 1 2 3]
drawT(TAZ4)
Thanks.
Walter Roberson
Walter Roberson 2014 年 1 月 27 日
Replace your calls to legend() with calls to legendflex() after you have downloaded legendflex() from the File Exchange.
Tomas
Tomas 2014 年 1 月 27 日
??? Error using ==> legendflex at 219 Legend labels must be a cell array of strings
Error in ==> drawT at 33 leg1 = legendflex(h1, get(h1, 'DisplayName'), 'anchor', {'ne','nw'}, 'buffer', [5 0]);
Error in ==> Untitled55 at 19 drawT(TAZ)
Kelly Kearney
Kelly Kearney 2014 年 1 月 27 日
If you create your plots (without legends) and then run the code I gave you verbatim, it should collect the proper handles and labels and generate the legends.
Otherwise, to troubleshoot, I'll need to see the actual code that you have run... what you've typed above has quite a few typos.
Tomas
Tomas 2014 年 1 月 27 日
I will send my function, start in command window for example: MON = [2.8 3.6 17.2; 5.4 8.3 15.8; 2.5 3.2 17.6]
drawT(MON)
Z1={{rand(1,3)},[{rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)} {rand(1,3)}],[{rand(1,3)} {rand(1,3)} ] }
drawZ(Z1)
I want to make it previewed in one plot, and their legends.
Thank you for your help
Tomas
Tomas 2014 年 1 月 27 日
one more question, when i have matrix 1x3
for example MON[1 2 3]
??? Error using ==> get Invalid handle
Error in ==> vykreslizhluk at 25
leg1 = legendflex(h1, get(h1, 'DisplayName'), 'anchor', {'ne','nw'}, 'buffer', [5 0]);
does not work for matrix(1x3) ?

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

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

タグ

質問済み:

2014 年 1 月 27 日

コメント済み:

2014 年 1 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by