Hi. I’m attempting to color code the legend (group) entries in a group scatter plot using the following code:
% Clear out all workspace variables and the command window
clear all;clc;% Set values for OATTs and IDs
OATT = [8;10;12;14;16;18;20];ID = [1;2;5;6;7;1;8];% Preallocate LegCol and color
LegCol = cell(1, size(ID, 1));color = cell(1, size(ID, 1));% Setup cases for legend
for idx = 1:size(ID, 1) switch ID(idx) case 1 LegCol{idx} = 'Wild-1'; color{idx} = [1 0 0]; % red
case 2 LegCol{idx} = 'Jets-2'; color{idx} = [0 1 0]; % green
case 3 LegCol{idx} = 'Kings-3'; color{idx} = [0 1 1]; % cyan
case 4 LegCol{idx} = 'Sharks-4'; color{idx} = [1 0 1]; % magenta
case 5 LegCol{idx} = 'Ducks-5'; color{idx} = [1 1 0]; % yellow
case 6 LegCol{idx} = 'Coyotes-6'; color{idx} = [0 0 0]; % black
case 7 LegCol{idx} = 'Canucks-7'; color{idx} = [0 0 1]; % blue
case 8 LegCol{idx} = 'Blackhawks-8'; color{idx} = [0.8 0.8 0.8]; % grey
case 9 LegCol{idx} = 'Blues-9'; color{idx} = [1 0.55 0]; % orange
case 10 LegCol{idx} = 'Predators-10'; color{idx} = [0.58 0 0.83]; % violet
endend% Transpode LegCol and color data for legend use
Legend_Nomenclature = transpose(LegCol);Color_Nomenclature = transpose(color);New_Colors = cell2mat(Color_Nomenclature);% Find unique elements of ID and use the returned index
% vector n as the y-axis value in the gscatter plot
[~, m, n] = unique(ID);ID_Index = n;figure('Name', 'Gscatter Test');h = gscatter(OATT, ID_Index, Legend_Nomenclature, New_Colors, 'o', 15, 'on');for n = 1:length(h) set(h(n), 'MarkerFaceColor', New_Colors(n, :));end
At this time, I have to apologize for the lack of an image as I’m unable to embed it in this question.
However, when I run the above code, the group scatter plot is created – as expected – with a single exception. For unknown reasons the last group of data (with ID = 8) shows up as a red dot on the plot and within the legend. It should in fact be grey.
What could be the cause of this?
Best Answer