Add multiple legends to graph

19 visualizaciones (últimos 30 días)
Hannah
Hannah el 10 de Ag. de 2021
Comentada: Hannah el 11 de Ag. de 2021
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?

Respuesta aceptada

Dave B
Dave B el 10 de Ag. de 2021
Editada: Dave B el 10 de Ag. de 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 comentarios
Dave B
Dave B el 10 de Ag. de 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"];
Hannah
Hannah el 11 de Ag. de 2021
Thank you Dave! This is super helpful!
Haihui

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by