필터 지우기
필터 지우기

Assigning same color for same value at different plots

조회 수: 16 (최근 30일)
Niraj Bal Tamang
Niraj Bal Tamang 2022년 4월 24일
댓글: Niraj Bal Tamang 2022년 4월 25일
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

채택된 답변

Chunru
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']
mt = 'o*^vs'
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
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,:))
Niraj Bal Tamang
Niraj Bal Tamang 2022년 4월 25일
The first solution worked. But when i tried this on another similar problem with the second solution, it is giving "The logical indices contain a true value outside of the array boundas.". I used 6 tables instead of 3 and two are empty (so i assigned nan to them). I want to include them as well to show there is no point distribution in those 2 plots. There are 16 names in this problem with maximum of 96 values in a table with random distribution of those names. And I changed the for i=1:3 to 1:6 and subplot(1,3,i) to subplot(3,2,i) as i want to plot them in 3rows and 2 columns. But this is not working for this problem although I am just changing the values and using the same solution as before.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by