Adding more circles to the scatter plot legend

조회 수: 7 (최근 30일)
Miriam Han
Miriam Han 2023년 6월 18일
편집: VBBV 2023년 6월 19일
I am using the following code and want to create a legend where different colors correspond to different dosage.
Currently, all the points are marked as the same color in the legend (attached)
dpli_dris = [1,2,3,4]
hub_dris = [1,2,3,3.5]
dosage_order = [1,2,3,4]
dosage = ['1 mcg/kg/min';'3 mcg/kg/min';'4 mcg/kg/min';'6 mcg/kg/min'];
handle = figure;
s=scatter(dpli_dris,hub_dris,[],'b','filled','o','MarkerEdgeColor',[0 0 0])
hold on
s.AlphaData = dosage_order;
s.MarkerFaceAlpha = 'flat';
leg = legend(dosage,'Location','northeastoutside');
What can I do to fix the legend?
Thank you

답변 (1개)

VBBV
VBBV 2023년 6월 19일
편집: VBBV 2023년 6월 19일
If you want different colors, call the scatter function seperately for each point. Currently, you are assigning the same color blue to all the 4 points in vectors dpli_dris & hub_dris using single scatter call.
dpli_dris = [1,2,3,4];
hub_dris = [1,2,3,3.5];
dosage_order = [1,2,3,4];
dosage = {'1 mcg/kg/min','3 mcg/kg/min','4 mcg/kg/min','6 mcg/kg/min'};
hold on
scatter(dpli_dris(1),hub_dris(1),'b','filled','o','MarkerEdgeColor',[0 0 0])
scatter(dpli_dris(2),hub_dris(2),'r','filled','o','MarkerEdgeColor',[0 0 0])
scatter(dpli_dris(3),hub_dris(3),'g','filled','o','MarkerEdgeColor',[0 0 0])
scatter(dpli_dris(4),hub_dris(4),'m','filled','o','MarkerEdgeColor',[0 0 0])
leg = legend(dosage,'Location','northwest'); grid
  댓글 수: 3
Miriam Han
Miriam Han 2023년 6월 19일
Yep. But I wanted to find a way to assign different transparency across the dosages instead of assigning a different color.
VBBV
VBBV 2023년 6월 19일
편집: VBBV 2023년 6월 19일
Use MarkerFaceAlpha as you did earlier but with a slight change
dpli_dris = [1,2,3,4];
hub_dris = [1,2,3,3.5];
dosage_order = [1,2,3,4];
dosage = {'1 mcg/kg/min','3 mcg/kg/min','4 mcg/kg/min','6 mcg/kg/min'};
% colors = {'b','r','g','m'};
Alpha = [0.2 0.4 0.6 0.8]
Alpha = 1×4
0.2000 0.4000 0.6000 0.8000
hold on
for k = 1:length(hub_dris)
scatter(dpli_dris(k),hub_dris(k),'b','filled','o','MarkerEdgeColor',[0 0 0],'MarkerFaceAlpha',Alpha(k))
end
leg = legend(dosage,'Location','northwest'); grid

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by