Calculation of a grouped two-dimensional vector in MATLAB

I am trying to plot a two-dimensional vector (2D plot). But I do not want all data points to have the same color on the plot. Each data set has a group. I want to have different colors for each group of datapoints.

class=[1 3 2 5 2 5 1 3 3 4 2 2 2]

says that each datapoint belongs to which group

X=[x1,y1;x2,y2;x3,y3;.....] 

the number of these data points matches the number of elements in the class vector.

Now I want to build them based on colors.

+3
source share
3 answers

You can use SCATTER to easily create data with different colors. I agree with @gnovice to use classIDinstead class, by the way.

scatter(X(:,1),X(:,2),6,classID); %# the 6 sets the size of the marker.

EDIT

, @yuk, @gnovice.

GSCATTER

%# plot data and capture handles to the points
hh=gscatter(randn(100,1),randn(100,1),randi(3,100,1),[],[],[],'on');
%# hh has an entry for each of the colored groups. Set the DisplayName property of each of them
set(hh(1),'DisplayName','some group')

%# create some data
X = randn(100,2);
classID = randi(2,100,1);
classNames = {'some group','some other group'}; %# one name per class
colors = hsv(2); %# use the hsv color map, have a color per class

%# open a figure and plot
figure
hold on
for i=1:2 %# there are two classes
id = classID == i;
plot(X(id,1),X(id,2),'.','Color',colors(i,:),'DisplayName',classNames{i})
end
legend('show')

, .

+4

-, CLASS , classID.

classID :

index = (classID == 1);            %# Logical index of where classID is 1
plot(X(index,1),X(index,2),'r.');  %# Plot all classID 1 values as a red dot
hold on;                           %# Add to the existing plot
+2

Also look at the GSCATTER function from the statistics toolbar. You can specify the color, size and symbol for each group only once.

gscatter(X(:,1),X(:,2),classID,'bgrcm');

or simply

gscatter(X(:,1),X(:,2),classID); %# groups by color by default
+2
source

Source: https://habr.com/ru/post/1744970/


All Articles