How do I change the color of the markers in the legend in a scatter plot with colormap?

31 visualizaciones (últimos 30 días)
Hi all!
I am trying to create a scatter plot, in which data corresponding to three groups (distinguished by marker shapes: diamond, square and circle) is displayed.
Per group, three variables are displayed: one on the x-axis (X), one on the y-axis (Y) and one using a color with colormap (Z) (see figure).
In the legend, I want to show which marker shape correspond to which group. However, in the legend, the colors of the marker shapes are also displayed. In other words, I want the marker shapes in the legend to be the same color, for example black, since I want the legend to distinguish between the marker shapes.
Does anyone know how I can do this?
Thanks!
Bas
XGroup1=[1.82532934086820;1.76189882802566;1.86581665452423;1.84208279284533;1.94543462454121;1.88077578111331;1.76128381214026;1.97822911370926;1.97304464195403;1.93378442521392;1.82355428238068;1.95574913450611;1.79888388726231;2.03695171405617;2.07770691698359;2.03077445964580;1.70330090422324;2.04374344957203;2.11550444932665;1.66891179277113];
YGroup1=[4.19510557104997;4.27895115323819;4.17607420453055;4.23946800537198;4.16076632696035;4.28714069261889;4.41054054949624;4.36008886122562;4.40613345496737;4.47328547002883;4.58682008385437;4.53263937164527;4.80391026547792;4.59490503897125;4.66637612785156;4.72464758867533;5.05757343613950;4.80660162439147;4.86233248346601;5.34359104874761];
ZGroup1=[28.9049953393461;27.0826445749260;30.7223210097028;32.5346215859960;30.7223210097026;32.1979402837428;25.2552687164425;31.8612589814898;31.8612589814895;31.5245776792366;23.4228677638953;33.3167778792759;23.4228677638953;32.9750714829591;32.9750714829591;32.6333650866427;21.5854417172847;32.2916586903261;34.0637585141114;19.7429905766107];
XGroup2=[4.11038901821562;4.15272906858114;4.11659427612502;4.07459217273351;3.96439621809489;4.24216878019657;3.90072245952305;4.35569830297761;3.88585124287385;4.33750567095077;4.36726370327498;3.92033090640804;4.44401766265253;3.81462506598804;4.56540621414188;3.72070015391738;4.57550038476600;4.60083639767166;3.67495838631681;4.67269963059073];
YGroup2=[8.99512147900667;8.99721611074594;9.03507142816632;9.11782064376342;9.24355007384221;9.04005040023745;9.41188305651848;9.12182969677837;9.62193756367002;9.24035899304924;9.37231785937738;9.87240596905548;9.50343401151885;10.1616509156319;9.63518782193602;10.4878056145294;9.77782674558925;9.91035995982594;10.8488687742030;10.0634237425620];
ZGroup2=[25.7555670883019;28.1615447756094;23.3408575658771;23.3408575658773;20.9174162083359;30.5587906277998;18.4852430156775;30.5587906278000;16.0443379879018;32.9473046448736;32.5019810539089;13.5947011250097;34.8730314007486;13.5947011250097;34.4189759746667;11.1363324270001;34.4189759746672;33.9649205485850;8.66923189387414;33.5108651225037];
XGroup3=[5.24162075972210;5.47774939866243;5.01051063344864;5.72814162826148;5.03132688985076;5.28659758051943;4.79785379062001;5.99437514354079;5.09142872084174;5.56794369577748;4.84043403851748;5.36626806096234;5.79222946142251;4.58594195840591;5.18942798130283;4.61032942769405;4.92150358673526;5.66789902399382;5.48207836446702;5.87857831229213];
YGroup3=[24.8502764468704;24.6597050259612;25.1715040581295;24.4592054332807;25.2301708089576;24.9894977529324;25.4897892071292;24.3239805363222;25.2908036788056;24.8270706380107;25.5883831613631;25.0626090231808;24.6533564057156;25.9043976095868;25.3534710003533;25.9456661841259;25.6401886937639;24.9149473000733;25.1381634456723;24.7552738859307];
ZGroup3=[118.298676248774;121.395642524695;116.742655469718;122.936588021560;118.298676248774;121.395642524695;113.615538629416;124.472508424362;119.849671933766;124.472508424362;116.742655469718;122.936588021560;126.003403733100;112.044442568170;121.395642524695;113.615538629417;118.298676248774;126.003403733100;124.472508424362;127.529273947775];
figure
scatter(XGroup1, YGroup1, 30, ZGroup1, 'o', 'filled', 'MarkerEdgeColor','k');
hold on
grid on
scatter(XGroup2, YGroup2, 30, ZGroup2, 's', 'filled', 'MarkerEdgeColor','k');
scatter(XGroup3, YGroup3, 30, ZGroup3, 'd', 'filled', 'MarkerEdgeColor','k');
h1=colorbar;
colormap(jet(10))
xlabel('X')
ylabel('Y')
ylabel(h1,'Z')
legend('Group 1', 'Group 2', 'Group 3')
question_matlab.jpg

Respuesta aceptada

Walter Roberson
Walter Roberson el 19 de Jun. de 2019
Most of the time, using legend() together with scatter() will not give you the results you want.
The work-around is to deliberately do a few more scatter() with nan as the coordinates and with the exact combination of properties that you want to show up in the legend, recording the handles as you do this, and then pass those handles in to legend() instead of having legend() look and find the first however-many handles of graphics objects.
  3 comentarios
Soumyajit Dey
Soumyajit Dey el 22 de En. de 2021
Editada: Soumyajit Dey el 22 de En. de 2021
I am working on a similar problem, trying to create a X-Y scatter plot where the colormap is a 2-d matrix Z. I can use 's','d' or 'o' as my marker type, but shows error when I try to use '|' (vertical line) as my marker. Can anyone help me with this? Thanks in advance.
scatter(Xe,Ye,20,Ze,'s');
% how to change 's' to '|'

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by