自动绘制不同颜色的线条
我试图在同一张图上绘制几个核密度估计,并且我希望它们都是不同的颜色。我有一个使用字符串 'rgbcmyk'
的拼凑解决方案,并针对每个单独的图逐步执行它,但在 7 次迭代后我开始出现重复。有没有更简单/更有效的方法来做到这一点,并且有更多的颜色选项?
for n=1:10
source(n).data=normrnd(rand()*100,abs(rand()*50),100,1); %generate random data
end
cstring='rgbcmyk'; % color string
figure
hold on
for n=1:length(source)
[f,x]=ksdensity(source(n).data); % calculate the distribution
plot(x,f,cstring(mod(n,7)+1)) % plot with a different color each time
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 HSV 等颜色图来生成一组颜色。例如:
MATLAB 有 13 种不同的命名颜色图('doc colormap' 列出了所有这些)。
以不同颜色绘制线条的另一个选项是使用 LineStyleOrder 属性;请参阅 MATLAB 文档中的定义绘图线条的颜色更多信息。
You could use a colormap such as HSV to generate a set of colors. For example:
MATLAB has 13 different named colormaps ('doc colormap' lists them all).
Another option for plotting lines in different colors is to use the LineStyleOrder property; see Defining the Color of Lines for Plotting in the MATLAB documentation for more information.
实际上,让颜色循环的一个不错的快捷方法是使用
hold all;
代替hold on;
。每个连续的绘图
都将通过 MATLAB 的默认颜色图进行旋转(自动为您旋转)。来自 MATLAB 网站
hold
:Actually, a decent shortcut method for getting the colors to cycle is to use
hold all;
in place ofhold on;
. Each successiveplot
will rotate (automatically for you) through MATLAB's default colormap.From the MATLAB site on
hold
:迟到的答案,但有两件事需要添加:
'ColorOrder'
属性以及如何使用'DefaultAxesColorOrder'
设置全局默认值的信息,请参阅“附录” ”在这篇文章的底部。ColorOrder
axes
属性允许 MATLAB 在使用hold on/all
时自动循环显示颜色列表(再次,请参阅下面的附录了解如何设置
/获取
针对特定轴的ColorOrder
或通过DefaultAxesColorOrder
进行全局设置)。然而,默认情况下,MATLAB 仅指定一个简短的颜色列表(从 R2013b 开始,只有 7) 进行循环,另一方面,找到一个好的颜色列表可能会出现问题。更多数据系列的颜色集。对于 10 个图,您显然不能依赖默认的 ColorOrder。定义 N 个视觉上不同的颜色的一个好方法是使用 在 MATLAB Central File Exchange 上提交“生成最大程度感知不同的颜色”(GMPDC)。用作者自己的话说是最好的描述:
例如,当请求 25 种颜色时:
在 MathWorks 上选择了 GMPDC 提交官方博客本周精选 2010 年,部分原因是能够请求任意数量的颜色(与 MATLAB 内置的 7 种默认颜色相反)。他们甚至提出了在启动时设置 MATLAB 的
ColorOrder
的极好建议,当然,您可以为单个轴设置
ColorOrder
或简单地生成要在中使用的颜色列表。任何你喜欢的方式。例如,要生成 10 种“最大感知上不同的颜色”并将它们用于同一轴上的 10 个绘图(但不使用ColorOrder
,因此需要循环):过程得到简化,不需要
for
循环,使用ColorOrder
轴属性:附录
获取
ColorOrder
用于当前轴的 RGB 数组,要获取新轴的默认
ColorOrder
,在 MATLAB start 上设置具有 10 种颜色的新全局
ColorOrder
的示例,在startup.m
:Late answer, but two things to add:
'ColorOrder'
property and how to set a global default with'DefaultAxesColorOrder'
, see the "Appendix" at the bottom of this post.The
ColorOrder
axes
property allows MATLAB to automatically cycle through a list of colors when usinghold on/all
(again, see Appendix below for how toset
/get
theColorOrder
for a specific axis or globally viaDefaultAxesColorOrder
). However, by default MATLAB only specifies a short list of colors (just 7 as of R2013b) to cycle through, and on the other hand it can be problematic to find a good set of colors for more data series. For 10 plots, you obviously cannot rely on the defaultColorOrder
.A great way to define N visually distinct colors is with the "Generate Maximally Perceptually-Distinct Colors" (GMPDC) submission on the MATLAB Central File File Exchange. It is best described in the author's own words:
For example, when 25 colors are requested:
The GMPDC submission was chosen on MathWorks' official blog as Pick of the Week in 2010 in part because of the ability to request an arbitrary number of colors (in contrast to MATLAB's built in 7 default colors). They even made the excellent suggestion to set MATLAB's
ColorOrder
on startup to,Of course, you can set the
ColorOrder
for a single axis or simply generate a list of colors to use in any way you like. For example, to generate 10 "maximally perceptually-distinct colors" and use them for 10 plots on the same axis (but not usingColorOrder
, thus requiring a loop):The process is simplified, requiring no
for
loop, with theColorOrder
axis property:APPENDIX
To get the
ColorOrder
RGB array used for the current axis,To get the default
ColorOrder
for new axes,Example of setting new global
ColorOrder
with 10 colors on MATLAB start, instartup.m
:聚会迟到了。我自己正在研究这个问题,刚刚发现了这个名为 ColorOrder 的轴选项
您可以指定会话的颜色顺序或仅指定图形的颜色顺序,然后绘制一个数组并让 MATLAB 自动循环指定的颜色。
请参阅更改默认颜色顺序
示例
Late to the party. I was looking into this myself and just found about this axes option called ColorOrder
you can specify the colour order for the session or just for the figure and then just plot an array and let MATLAB automatically cycle through the colours specified.
see Changing the Default ColorOrder
example
如果所有向量具有相同的大小,则创建一个矩阵并绘制它。
每列自动用不同的颜色绘制
然后您可以使用
legend
来指示列:或者,如果您有一个带有内核名称的单元格,请使用
If all vectors have equal size, create a matrix and plot it.
Each column is plotted with a different color automatically
Then you can use
legend
to indicate columns:Or, if you have a cell with kernels names, use