Why am I getting redundant legend (group) icon colors when utilizing the gscatter command?

3 views (last 30 days)
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
end
end
% 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?

Accepted Answer

Tom Lane
Tom Lane on 9 Feb 2015
The gscatter function as a separate legend entry for each group. Your group, as given by the Legend_Nomenclature variable, is the same for the points at (8,1) and (18,1).
You might just want to use the plot function if you intend for each point to have a separate legend entry.
  1 Comment
Brad
Brad on 11 Feb 2015
Tom, I believe you are correct. As hard as I try to get the legend correct using the gscatter function, it just won't do it. The plot function does work fine. Thanks for taking a look at this.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!