ポイント のセット(行列Nx1)とこのポイントのグループ(行列Nx1)があります。この点をプロットしたいのですが(問題ありません、こうしますplot(points, groups, 'o');
:)、グループごとに固有の色を設定したいと思います。これどうやってするの?現在、私には2つのグループ(1,2)しかありません。
9647 次
3 に答える
3
論理インデックスを使用して、必要なポイントを選択します
figure;
hold all; % keep old plots and cycle through colors
ind = (groups == 1); % select all points where groups is 1
% you can do all kind of logical selections here:
% ind = (groups < 5)
plot(points(ind), groups(ind), 'o');
于 2011-07-21T12:34:25.580 に答える
2
いくつかのランダムなデータが与えられた場合:
points = randn(100,1);
groups = randi([1 2],[100 1]);
ここにいくつかのより一般的な提案があります:
g = unique(groups); %# unique group values
clr = hsv(numel(g)); %# distinct colors from HSV colormap
h = zeros(numel(g),1); %# store handles to lines
for i=1:numel(g)
ind = (groups == g(i)); %# indices where group==k
h(i,:) = line(points(ind), groups(ind), 'LineStyle','none', ...
'Marker','.', 'MarkerSize',15, 'Color',clr(i,:));
end
legend(h, num2str(g,'%d'))
set(gca, 'YTick',g, 'YLim',[min(g)-0.5 max(g)+0.5], 'Box','on')
xlabel('Points') ylabel('Groups')
Statistics Toolboxにアクセスできる場合は、上記のすべてを1回の呼び出しで簡略化する関数があります。
gscatter(points, groups, groups)
最後に、この場合、箱ひげ図を表示する方が適切です。
labels = num2str(unique(groups),'Group %d');
boxplot(points,groups, 'Labels',labels)
ylabel('Points'), title('Distribution of points across groups')
于 2011-07-21T14:33:05.807 に答える
0
グループの数が事前にわかっていると仮定すると、次のようになります。
plot(points(find(groups == 1)), groups(find(groups == 1)), points(find(groups == 2)), groups(find(groups == 2)));
find
groups
条件が成立するすべてのインデックスが表示されます。の出力を両方のサブベクトルとして使用し、の可能find
な値ごとに使用します。points
groups
groups
plot
複数のxyの組み合わせをプロットするために使用する場合、それぞれに異なる色を使用します。
または、各色を明示的に選択することもできます。
hold on
plot(points(find(groups == 1)), groups(find(groups == 1)), 'r')
plot(points(find(groups == 2)), groups(find(groups == 2)), 'y')
最後に、色を自動的に循環するようにプロットに指示する方法があります。これにより、色plot
を指定せずに呼び出すことができますが、この方法ではわかりません。
于 2011-07-21T12:16:20.527 に答える