Add multiple legends to graph
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
0 commentaires
Réponse acceptée
Dave B
le 10 Août 2021
Modifié(e) : Dave B
le 10 Août 2021
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 commentaires
Dave B
le 10 Août 2021
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"];
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Legend dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!