plot3 two legends in one plot3
显示 更早的评论
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.
采纳的回答
更多回答(1 个)
Kelly Kearney
2014-1-27
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
2014-1-27
编辑:Walter Roberson
2014-1-27
Walter Roberson
2014-1-27
Replace your calls to legend() with calls to legendflex() after you have downloaded legendflex() from the File Exchange.
Tomas
2014-1-27
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
2014-1-27
Tomas
2014-1-27
类别
在 帮助中心 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!