MATLAB - 用补丁绘制多条未连接的线并指定线条颜色

MATLAB - plot multiple unconnected lines with patch and specify linecolor

我想使用补丁在一个命令中绘制多条未连接的线。这些线有不同的颜色,我想指定。不幸的是,补丁在黄色到蓝色区域使用随机颜色而不是我定义的颜色 - 我该如何解决这个问题?

clear all
close all
cla;

x=[];
y=[];
CC=[];
n=10;
for i=1:n
    % define start and end from i-th line
    x(end+1)=i;
    x(end+1)=i+1;
    % add nan to seperate lines
    x(end+1)=nan;

    % define start and end from i-th line
    y(end+1)=i;
    y(end+1)=1;
    % add nan to seperate lines
    y(end+1)=nan;

    CC(end+1,1:3)=0.1.*i;
end

%funktioniert
figure(1)
subplot(1,2,1)
h = patch(x', y', 0);
set(h,'LineWidth',2);
set(h,'cdata', CC, 'edgecolor','flat','facecolor','none')
title('wrong colors')

subplot(1,2,2)
for i=1:n
    xx=[1:3];
    yy=[i i i];
    line(xx,yy,'Color',CC(i,:))
    hold on
end
title('wanted colors')

非常感谢! 斯迈卡

如果您想绘制线条,请使用专用于此目的的函数,plot or line, instead of using patch. Just change the 'ColorOrder' 到您想要的颜色。

n = 10;
%Starting and ending point of lines in each col of x and y
x = [1:n; 2:n+1];    
y = [1:n; ones(1,n)]; 
CC = repmat(0.1:0.1:1,3,1).';   %Required colors
%Changing ColorOrder to required colors ( and turning the box (optional) )
set(gca, 'ColorOrder', CC, 'box', 'on');
line(x,y);    %or plot(x,y)

结果: