我想繪製一個二維矢量(2D圖)的圖。但我不希望所有的數據點在情節上具有相同的顏色。每個數據點對應一個組。我想爲每組數據點設置不同的顏色。在MATLAB中繪製分組的二維矢量
class=[1 3 2 5 2 5 1 3 3 4 2 2 2]
說每個數據點屬於哪個組
X=[x1,y1;x2,y2;x3,y3;.....]
這些數據點的數目是相同的類中的矢量元素的數量。
現在我要繪製這些基礎上的顏色。
我想繪製一個二維矢量(2D圖)的圖。但我不希望所有的數據點在情節上具有相同的顏色。每個數據點對應一個組。我想爲每組數據點設置不同的顏色。在MATLAB中繪製分組的二維矢量
class=[1 3 2 5 2 5 1 3 3 4 2 2 2]
說每個數據點屬於哪個組
X=[x1,y1;x2,y2;x3,y3;.....]
這些數據點的數目是相同的類中的矢量元素的數量。
現在我要繪製這些基礎上的顏色。
您可以使用SCATTER輕鬆繪製不同的顏色數據。順便說一下,我同意使用classID
代替class
,@gnovice。
scatter(X(:,1),X(:,2),6,classID); %# the 6 sets the size of the marker.
編輯
如果你想顯示一個傳奇,你必須要麼使用@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')
你也可能想看看grouped data如果你有統計工具箱。
首先,因爲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
謝謝,我發現這也是,但你最好: gscatter(M(:,2),M(:,1)的classID, 'brgyckm', 'O')。並感謝您對「班級」的評論。 – Hossein 2010-05-11 17:53:12
看還從統計工具箱的GSCATTER功能。您可以爲每個組指定顏色,大小和符號一次。
gscatter(X(:,1),X(:,2),classID,'bgrcm');
或只是
gscatter(X(:,1),X(:,2),classID); %# groups by color by default
+1:酷!我從未注意到SCATTER的第四個輸入。 – gnovice 2010-05-11 17:04:28
有趣的是,您可以使用與classID相同的長度的矢量作爲第三個參數,以按符號大小區分類。嘗試'scatter(X(:,1),X(:,2),classID * 1000,'r。')'繪製氣泡圖。 – yuk 2010-05-11 17:37:55
謝謝,我發現這也是,但你最好: gscatter(M(:,2),M(:,1)的classID, 'brgyckm', 'O') – Hossein 2010-05-11 17:53:47