Assigning same color for same value at different plots

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

Thank you very much. It worked. One more question related to this. If I have more number of names such as 20 or more, how can i assign the colors and symbols in such case as it would be difficult to look for different symbols and color codes as well.
Since the applicable name should not exceed the number of data points, you can modify the following line:
% idx = ismember(name{i}, uname(j));
idx = ismember(name{i}(1:length(x{i})), uname(j));
Thank you. I was trying to say if I have a 94*3 table with x,y values and 20 names distributed among them, how can I assign them as x{1},y{1} and name{1}. I can create a variable for each from the column and assign them here but I am confused in the uname,lc and mt part where I can write the 20 names there but am confused on how to assign 20 different colors and symbols for the plot.
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,:))
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개)

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

태그

질문:

2022년 4월 24일

댓글:

2022년 4월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by