matlab绘制不同颜色

发布于 2024-11-25 16:57:11 字数 178 浏览 0 评论 0原文

我有一组(矩阵Nx1)和该点的(矩阵Nx1)。我想绘制这些点(没有问题,我这样做:plot(points, groups, 'o');),但我想为每个组设置唯一的颜色。我该怎么做?现在我只有两个组(1,2)。

I have set of points (matrix Nx1) and groups for this points (matrix Nx1). I want to plot this points (there is no problem, I do this like this: plot(points, groups, 'o');), but I'd like to set unique color for each group. How can I do this? Now I have only two groups (1,2).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断念 2024-12-02 16:57:11

使用逻辑索引来选择所需的点

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');

Use logical indexing to select the points you want

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');
带刺的爱情 2024-12-02 16:57:11

给定一些随机数据:

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')

scatter_lines

如果您有权访问统计工具箱,则有一个在一次调用中简化上述所有内容的函数:

gscatter(points, groups, groups)

最后,在这种情况下,显示 盒子绘图

labels = num2str(unique(groups),'Group %d');
boxplot(points,groups, 'Labels',labels)
ylabel('Points'), title('Distribution of points across groups')

boxplot

Given some random data:

points = randn(100,1);
groups = randi([1 2],[100 1]);

Here are a few more general suggestions:

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')

scatter_lines

If you have access to the Statistics Toolbox, there is a function that simplifies all of the above in one call:

gscatter(points, groups, groups)

Finally, in this case, it would be more suitable to display the Box plot:

labels = num2str(unique(groups),'Group %d');
boxplot(points,groups, 'Labels',labels)
ylabel('Points'), title('Distribution of points across groups')

boxplot

Hello爱情风 2024-12-02 16:57:11

假设先验已知组数:

plot(points(find(groups == 1)), groups(find(groups == 1)), points(find(groups == 2)), groups(find(groups == 2)));

find 将为您提供满足条件的所有 索引。对于 groups 的每个可能值,您可以使用 find 的输出作为 pointsgroups 的子向量。

当您使用 plot 绘制多个 xy 组合时,它会为每个组合使用不同的颜色。

或者,您可以明确地选择每种颜色:

hold on
plot(points(find(groups == 1)), groups(find(groups == 1)), 'r')
plot(points(find(groups == 2)), groups(find(groups == 2)), 'y')

最后,有一种方法可以告诉绘图自动循环显示颜色,这样您就可以在不指定颜色的情况下调用绘图,但该方法让我困惑。

Assuming the number of groups is known a-priori:

plot(points(find(groups == 1)), groups(find(groups == 1)), points(find(groups == 2)), groups(find(groups == 2)));

find will give you all the indices of groups for which the condition holds. You use the output of find as a subvector of both points and groups for each possible value of groups.

When you use plot to plot more than one x-y combination, it uses a different color for each.

Alternatively, you could just choose each color explicitly:

hold on
plot(points(find(groups == 1)), groups(find(groups == 1)), 'r')
plot(points(find(groups == 2)), groups(find(groups == 2)), 'y')

Finally, there's a way to tell plot to cycle through the colors automatically, so that you can call plot without specifying a color, but the method eludes me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文