Assigning same color for same value at different plots
조회 수: 16 (최근 30일)
이전 댓글 표시
I have 3 tables.
For table1, x=[0.1 0.4 0.6 0.8], y=[0.2 0.3 0.7 0.9], name=[A B C A]
For table 2, x=[0.1 0.2 0.4 0.6 0.5 0.8], y=[0.2 0.5 0.3 0.4 0.7 0.9], name=[A B C A D E]
For table 3, x=[0.1 0.2 0.4 0.6 0.5 0.8 0.2 0.5 0.3 0.4 0.7 0.9 ], y=[0.2 0.5 0.3 0.4 0.7 0.9 0.1 0.2 0.4 0.6 0.5 0.8], name=[A B C A D E B C A D E B E B C A D]
I want to plot these points such that they have the same color and markertype assigned based upon their names for all three figures. I tried ploting them using gscatter plot with the 'name' as the group variable but the colors are different for same name in different figures. Can anyone please help me to assign same marker type and color for each name value and get a common legend for all three plots.
Thank You
댓글 수: 0
채택된 답변
Chunru
2022년 4월 25일
x{1}=[0.1 0.4 0.6 0.8];
y{1}=[0.2 0.3 0.7 0.9];
name{1}=["A" "B" "C" "A"];
x{2}=[0.1 0.2 0.4 0.6 0.5 0.8];
y{2}=[0.2 0.5 0.3 0.4 0.7 0.9];
name{2}=["A" "B" "C" "A" "D" "E"];
x{3}=[0.1 0.2 0.4 0.6 0.5 0.8 0.2 0.5 0.3 0.4 0.7 0.9 ];
y{3}=[0.2 0.5 0.3 0.4 0.7 0.9 0.1 0.2 0.4 0.6 0.5 0.8];
name{3}=["A" "B" "C" "A" "D" "E" "B" "C" "A" "D" "E" "B"]; % "E" "B" "C" "A" "D"];
% Colors & marker typescorresponding to the name A-E:
uname = ["A" "B" "C" "D" "E"];
lc = ['r' 'g' 'b' 'y' 'k'];
mt = ['o' '*' '^' 'v' 's']
for i =1 :3
subplot(1,3,i);
hold on
for j=1:length(lc)
idx = ismember(name{i}, uname(j));
plot(x{i}(idx), y{i}(idx), [mt(j) lc(j)], 'MarkerFaceColor', lc(j));
end
hold off
end
댓글 수: 5
Chunru
2022년 4월 25일
The uname can be obtained:
uname = unique(name{1})
For lc (line color) and mt (marker type): Matlab has around 15 differnt types of marker and 8 types of color (doc plot). Then you can use different comnibation of them to make 20 different lc-mt. You may what to have an array such as
lcmt = ['rp' 'g<' ... 'ko']
Altrenatively if you do want to use different colors for different names, you can control the color individually. (For marker type, you have to recycle them for 20 names). For example
c = jet(20); % make 20 different colors from jet colormap (20*3 array)
plot(x{i}(idx), y{i}(idx), 'Marker', mt(j), 'MarkEdgeColor', c(j, :), 'MarkerFaceColor', c(j,:))
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!