自动绘制不同颜色的线条

发布于 2024-08-17 14:09:38 字数 477 浏览 10 评论 0 原文

我试图在同一张图上绘制几个核密度估计,并且我希望它们都是不同的颜色。我有一个使用字符串 '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

I'm trying to plot several kernel density estimations on the same graph, and I want them to all be different colors. I have a kludged solution using a string 'rgbcmyk' and stepping through it for each separate plot, but I start having duplicates after 7 iterations. Is there an easier/more efficient way to do this, and with more color options?

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 技术交流群。

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

发布评论

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

评论(5

偏闹i 2024-08-24 14:09:38

您可以使用 HSV 等颜色图来生成一组颜色。例如:

cc=hsv(12);
figure; 
hold on;
for i=1:12
    plot([0 1],[0 i],'color',cc(i,:));
end

MATLAB 有 13 种不同的命名颜色图('doc colormap' 列出了所有这些)。

以不同颜色绘制线条的另一个选项是使用 LineStyleOrder 属性;请参阅 MATLAB 文档中的定义绘图线条的颜色更多信息。

You could use a colormap such as HSV to generate a set of colors. For example:

cc=hsv(12);
figure; 
hold on;
for i=1:12
    plot([0 1],[0 i],'color',cc(i,:));
end

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.

天荒地未老 2024-08-24 14:09:38

实际上,让颜色循环的一个不错的快捷方法是使用 hold all; 代替 hold on;。每个连续的绘图都将通过 MATLAB 的默认颜色图进行旋转(自动为您旋转)。

来自 MATLAB 网站 hold

hold all 保存绘图以及当前线条颜色和线条样式,以便后续绘图命令不会将 ColorOrder 和 LineStyleOrder 属性值重置为列表的开头。绘图命令从列表中最后一个绘图停止的位置继续循环浏览预定义的颜色和线条样式。

Actually, a decent shortcut method for getting the colors to cycle is to use hold all; in place of hold on;. Each successive plot will rotate (automatically for you) through MATLAB's default colormap.

From the MATLAB site on hold:

hold all holds the plot and the current line color and line style so that subsequent plotting commands do not reset the ColorOrder and LineStyleOrder property values to the beginning of the list. Plotting commands continue cycling through the predefined colors and linestyles from where the last plot stopped in the list.

清秋悲枫 2024-08-24 14:09:38

迟到的答案,但有两件事需要添加:

  • 有关如何更改 'ColorOrder' 属性以及如何使用 'DefaultAxesColorOrder' 设置全局默认值的信息,请参阅“附录” ”在这篇文章的底部。
  • MATLAB Central File Exchange 上有一个很棒的工具,可以生成任意数量的视觉上不同的颜色(如果您有图像处理工具箱可以使用它)。请继续阅读以了解详细信息。

ColorOrder axes 属性允许 MATLAB 在使用 hold on/all 时自动循环显示颜色列表(再次,请参阅下面的附录了解如何设置/获取针对特定轴的 ColorOrder 或通过 DefaultAxesColorOrder 进行全局设置)。然而,默认情况下,MATLAB 仅指定一个简短的颜色列表(从 R2013b 开始,只有 7 进行循环,另一方面,找到一个好的颜色列表可能会出现问题。更多数据系列的颜色集。对于 10 个图,您显然不能依赖默认的 ColorOrder。

定义 N 个视觉上不同的颜色的一个好方法是使用 在 MATLAB Central File Exchange 上提交“生成最大程度感知不同的颜色”(GMPDC)。用作者自己的话说是最好的描述:

此函数生成一组可通过参考“Lab”颜色空间进行区分的颜色,该颜色空间更接近比 RGB 更符合人类的色彩感知。给定一个可能颜色的初始大列表,它会迭代地选择列表中距离所有先前选择的条目最远(在实验室空间中)的条目。

例如,当请求 25 种颜色时:

25

在 MathWorks 上选择了 GMPDC 提交官方博客本周精选 2010 年,部分原因是能够请求任意数量的颜色(与 MATLAB 内置的 7 种默认颜色相反)。他们甚至提出了在启动时设置 MATLAB 的 ColorOrder 的极好建议,

distinguishable_colors(20)

当然,您可以为单个轴设置 ColorOrder 或简单地生成要在中使用的颜色列表。任何你喜欢的方式。例如,要生成 10 种“最大感知上不同的颜色”并将它们用于同一轴上的 10 个绘图(但不使用 ColorOrder,因此需要循环

% Starting with X of size N-by-P-by-2, where P is number of plots
mpdc10 = distinguishable_colors(10) % 10x3 color list
hold on
for ii=1:size(X,2),
    plot(X(:,ii,1),X(:,ii,2),'.','Color',mpdc10(ii,:));
end

:过程得到简化,不需要 for 循环,使用 ColorOrder 轴属性

% X of size N-by-P-by-2
mpdc10 = distinguishable_colors(10)
ha = axes; hold(ha,'on')
set(ha,'ColorOrder',mpdc10)    % --- set ColorOrder HERE ---
plot(X(:,:,1),X(:,:,2),'-.')   % loop NOT needed, 'Color' NOT needed. Yay!

附录

获取 ColorOrder 用于当前轴的 RGB 数组,

get(gca,'ColorOrder')

要获取新轴的默认 ColorOrder

get(0,'DefaultAxesColorOrder')

在 MATLAB start 上设置具有 10 种颜色的新全局 ColorOrder 的示例,在startup.m

set(0,'DefaultAxesColorOrder',distinguishable_colors(10))

Late answer, but two things to add:

  • For information on how to change the 'ColorOrder' property and how to set a global default with 'DefaultAxesColorOrder', see the "Appendix" at the bottom of this post.
  • There is a great tool on the MATLAB Central File Exchange to generate any number of visually distinct colors, if you have the Image Processing Toolbox to use it. Read on for details.

The ColorOrder axes property allows MATLAB to automatically cycle through a list of colors when using hold on/all (again, see Appendix below for how to set/get the ColorOrder for a specific axis or globally via DefaultAxesColorOrder). 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 default ColorOrder.

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:

This function generates a set of colors which are distinguishable by reference to the "Lab" color space, which more closely matches human color perception than RGB. Given an initial large list of possible colors, it iteratively chooses the entry in the list that is farthest (in Lab space) from all previously-chosen entries.

For example, when 25 colors are requested:

25 "maximally perceptually-distinct colors"

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,

distinguishable_colors(20)

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 using ColorOrder, thus requiring a loop):

% Starting with X of size N-by-P-by-2, where P is number of plots
mpdc10 = distinguishable_colors(10) % 10x3 color list
hold on
for ii=1:size(X,2),
    plot(X(:,ii,1),X(:,ii,2),'.','Color',mpdc10(ii,:));
end

The process is simplified, requiring no for loop, with the ColorOrder axis property:

% X of size N-by-P-by-2
mpdc10 = distinguishable_colors(10)
ha = axes; hold(ha,'on')
set(ha,'ColorOrder',mpdc10)    % --- set ColorOrder HERE ---
plot(X(:,:,1),X(:,:,2),'-.')   % loop NOT needed, 'Color' NOT needed. Yay!

APPENDIX

To get the ColorOrder RGB array used for the current axis,

get(gca,'ColorOrder')

To get the default ColorOrder for new axes,

get(0,'DefaultAxesColorOrder')

Example of setting new global ColorOrder with 10 colors on MATLAB start, in startup.m:

set(0,'DefaultAxesColorOrder',distinguishable_colors(10))
靖瑶 2024-08-24 14:09:38

聚会迟到了。我自己正在研究这个问题,刚刚发现了这个名为 ColorOrder 的轴选项
您可以指定会话的颜色顺序或仅指定图形的颜色顺序,然后绘制一个数组并让 MATLAB 自动循环指定的颜色。

请参阅更改默认颜色顺序

示例

set(0,'DefaultAxesColorOrder',jet(5))
A=rand(10,5);
plot(A);

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

set(0,'DefaultAxesColorOrder',jet(5))
A=rand(10,5);
plot(A);
紫南 2024-08-24 14:09:38

如果所有向量具有相同的大小,则创建一个矩阵并绘制它。
每列自动用不同的颜色绘制
然后您可以使用 legend 来指示列:

data = randn(100, 5);

figure;
plot(data);

legend(cellstr(num2str((1:size(data,2))')))

或者,如果您有一个带有内核名称的单元格,请使用

legend(names)

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:

data = randn(100, 5);

figure;
plot(data);

legend(cellstr(num2str((1:size(data,2))')))

Or, if you have a cell with kernels names, use

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