Matlab选择随机颜色进行绘图

发布于 2024-09-08 21:04:42 字数 213 浏览 1 评论 0原文

我有 6 个想要绘制的向量。 如何使每个图具有不同的颜色(随机)? 使用下面的代码,绘图将所有六个向量限制为一种颜色。

plot(x,y,'-rs','LineWidth',1,...
      'MarkerEdgeColor','k',...
      'MarkerFaceColor','g',...
      'MarkerSize',5);

I have 6 vectors which I want to plot.
How I can make each plot with different color (random)?
With the code below, the plot limited to one color for all six vectors.

plot(x,y,'-rs','LineWidth',1,...
      'MarkerEdgeColor','k',...
      'MarkerFaceColor','g',...
      'MarkerSize',5);

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

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

发布评论

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

评论(2

怼怹恏 2024-09-15 21:04:42

您可以让 PLOT 自动为您选择线条颜色。如果所有 6 个向量的长度相同,则可以将 x 和 y 坐标放入 N×6 矩阵 XY 并将它们传递给 绘图。每列将使用不同的颜色:

plot(X,Y,'-s');  %# Plots lines with square markers

您还可以使用一些内置的 colormaps 生成一组颜色,然后在单独绘制每条线时使用这些颜色。例如:

cmap = hsv(6);  %# Creates a 6-by-3 set of colors from the HSV colormap
for i = 1:6     %# Loop 6 times
  plot(X(:,i),Y(:,i),'-s','Color',cmap(i,:));  %# Plot each column with a
                                               %#   different color
end

You can have PLOT automatically choose line colors for you. If all 6 of your vectors are the same length, you can put the x and y coordinates into N-by-6 matrices X and Y and pass these to PLOT. A different color will be used for each column:

plot(X,Y,'-s');  %# Plots lines with square markers

You could also use some of the built-in colormaps to generate a set of colors, then use these when you plot each line separately. For example:

cmap = hsv(6);  %# Creates a 6-by-3 set of colors from the HSV colormap
for i = 1:6     %# Loop 6 times
  plot(X(:,i),Y(:,i),'-s','Color',cmap(i,:));  %# Plot each column with a
                                               %#   different color
end
萌无敌 2024-09-15 21:04:42

要创建随机颜色图,您可以执行以下操作

myMap = rand(nbColors, 3);
for i = 1:nbColors
  plot(X(:,i),Y(:,i),'-s','Color',myMap(i,:));
end

但是,正如我在对 gnovice 的答案的评论中所述,从颜色图中选择颜色通常会提供更易读的颜色组合。

To create a random color map, you could do the following

myMap = rand(nbColors, 3);
for i = 1:nbColors
  plot(X(:,i),Y(:,i),'-s','Color',myMap(i,:));
end

However, as I stated in my comment to gnovice's answer, picking colors out of a colormap generally provides much more readable color combinations.

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