Add multiple legends to graph
12 ビュー (過去 30 日間)
古いコメントを表示
I see a scatter plot that use different shapes of marker to indicate the source of data while using different color to indicate the area that each data point cover. I am able to produce the scatters but I don't know how to orgnize the legend like that.
If I use the default legend function in MatLab, it will give me all the combination of markers, which is lengthy.
Anyone know how to display the shape of marker and the color of marker seperately?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/708567/image.png)
0 件のコメント
採用された回答
Dave B
2021 年 8 月 10 日
編集済み: Dave B
2021 年 8 月 10 日
You can't (easily) make multiple legends, but you can make a 'fake' legend by including some objects in your chart that have NaN data and telling legend to use those objects:
% Fake data: 3 colors by 3 markers, would be 9 legend items
scatter(randn(10,1),randn(10,1),'ro')
hold on
scatter(randn(10,1),randn(10,1),'bo')
scatter(randn(10,1),randn(10,1),'mo')
scatter(randn(10,1),randn(10,1),'r*')
scatter(randn(10,1),randn(10,1),'b*')
scatter(randn(10,1),randn(10,1),'m*')
scatter(randn(10,1),randn(10,1),'rs')
scatter(randn(10,1),randn(10,1),'bs')
scatter(randn(10,1),randn(10,1),'ms')
% Make some data just for legend:
hLeg = gobjects(6,1);
% I used a . marker for colors, maybe a good idea to use something not in
% your 'real' markers
hLeg(1) = scatter(nan,nan,'r.');
hLeg(2) = scatter(nan,nan,'b.');
hLeg(3) = scatter(nan,nan,'m.');
% Similarly I used black for markers
hLeg(4) = scatter(nan,nan,'ko');
hLeg(5) = scatter(nan,nan,'k*');
hLeg(6) = scatter(nan,nan,'ks');
legend(hLeg, 'red', 'blue', 'magenta', 'circle', 'asterisk', 'square')
2 件のコメント
Dave B
2021 年 8 月 10 日
Similar approach with a mixed legend and colobrar:
scatter(randn(10,1),randn(10,1),40,randi(3,10,1),'o','filled')
hold on
scatter(randn(10,1),randn(10,1),40,randi(3,10,1),'*')
scatter(randn(10,1),randn(10,1),40,randi(3,10,1),'s','filled')
colormap(lines(3))
hLeg = gobjects(3,1);
hLeg(1) = scatter(nan,nan,'ko','filled');
hLeg(2) = scatter(nan,nan,'k*');
hLeg(3) = scatter(nan,nan,'ks','filled');
leg=legend(hLeg, 'circle', 'asterisk', 'square');
c=colorbar;
c.Ticks = [4/3 2 8/3];
c.TickLabels = ["color 1" "color 2" "color 3"];
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!