2017-10-06 103 views
-1

我想制作一堆具有相同颜色的图(或子图),然后前进到下一个颜色,再次绘制一堆图,等等。使用默认颜色很好。此外,我的图的数量可能会超过默认的颜色数量,所以我需要循环浏览它。我怎样才能做到这一点?在Matlab中,如何在绘图时控制颜色迭代器?

回答

0

会这样的东西满足您的要求?如果有很多颜色频繁变化的图表,可以创建一个包含索引时间(当出现颜色切换时)作为关键字和颜色作为值的字典。下面的代码更简单。

% Colors for 10 red plots and 5 black plots 
colors = [repmat('r', 1, 10), repmat('b', 1, 5)]; 
for i = 1:length(colors) 
    figure; plot(x, y, colors(i)); 
end 
+0

没有那就没办法了,因为它不是预先定义的,我会多少地块为每个颜色。 – LWZ

+0

MATLAB中的注释不使用#符号,您可能想要更改 – xrr

0

每当你的情节,你可能只是随机选择颜色:

% Create a random RGB color 
color = rand(1,3); 

% Plot as many times as needed with the newly created random color 
plot (X,Y,'color', color) 

可以遍历这个必要

0

当你创建一个轴多次,它具有属性ColorOrderIndex它指的是下一个要使用的颜色的索引。 ColorOrderIndex的初始值是1,并且每次向相同的坐标轴添加一个新图时(没有清除,即使用hold on),该值将增加并循环显示所有颜色(它们本身存储在属性ColorIndex作为具有三列的二维矩阵,其中每一行代表一个RGB三元组,这个矩阵有一个默认值,由7种颜色组成(在Matlab R2015a中),但你可以指定其他任何你想要的颜色)。 因此,通过手动确保ColorOrderIndex属性的值不增加,可以为下一个图保留相同的颜色。

ax = axes; 
hold on; 
numberOfColors = length(ax.ColorOrder); 
ax.ColorOrderIndex = 1; 

plot([0,0],[0,1],'LineWidth',2); 
ax.ColorOrderIndex = mod(ax.ColorOrderIndex-2,numberOfColors)+1; % keep same color 
plot([1,1],[0,1],'LineWidth',2); 
ax.ColorOrderIndex = mod(ax.ColorOrderIndex-2,numberOfColors)+1; % keep same color 
plot([2,2],[0,1],'LineWidth',2); 

% change color 
plot([3,3],[0,1],'LineWidth',2); 
ax.ColorOrderIndex = mod(ax.ColorOrderIndex-2,numberOfColors)+1; % keep same color 
plot([4,4],[0,1],'LineWidth',2); 


% change color 
plot([5,5],[0,1],'LineWidth',2); 
ax.ColorOrderIndex = mod(ax.ColorOrderIndex-2,numberOfColors)+1; % keep same color 
plot([6,6],[0,1],'LineWidth',2); 
ax.ColorOrderIndex = mod(ax.ColorOrderIndex-2,numberOfColors)+1; % keep same color 
plot([7,7],[0,1],'LineWidth',2); 
ax.ColorOrderIndex = mod(ax.ColorOrderIndex-2,numberOfColors)+1; % keep same color 
plot([8,8],[0,1],'LineWidth',2); 

% and so on... 

Output of above code