我正在尝试在同一张图上绘制多个核密度估计值,我希望它们都是不同的颜色。我有一个使用字符串 '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
最佳答案
您可以使用诸如 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 属性;见Defining the Color of Lines for Plotting在 MATLAB 文档中获取更多信息。
关于matlab - 自动绘制不同颜色的线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2028818/