matlab绘制不同颜色
我有一组点(矩阵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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用逻辑索引来选择所需的点
Use logical indexing to select the points you want
给定一些随机数据:
以下是一些更一般的建议:
如果您有权访问统计工具箱,则有一个在一次调用中简化上述所有内容的函数:
最后,在这种情况下,显示 盒子绘图:
Given some random data:
Here are a few more general suggestions:
If you have access to the Statistics Toolbox, there is a function that simplifies all of the above in one call:
Finally, in this case, it would be more suitable to display the Box plot:
假设先验已知组数:
find
将为您提供满足条件的所有组
索引。对于groups
的每个可能值,您可以使用find
的输出作为points
和groups
的子向量。当您使用
plot
绘制多个 xy 组合时,它会为每个组合使用不同的颜色。或者,您可以明确地选择每种颜色:
最后,有一种方法可以告诉绘图自动循环显示颜色,这样您就可以在不指定颜色的情况下调用绘图,但该方法让我困惑。
Assuming the number of groups is known a-priori:
find
will give you all the indices ofgroups
for which the condition holds. You use the output offind
as a subvector of bothpoints
andgroups
for each possible value ofgroups
.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:
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.