Matlab彩色地图线图

Matlab彩色地图线图,matlab,plot,line,colormap,Matlab,Plot,Line,Colormap,我正在尝试使用colormap为绘图上的线条指定颜色。每行的数据都是从文件生成的,每次导入的文件数/打印的行数都是可变的。我的代码是: d = uigetdir(pwd, 'Select a folder'); files = dir(fullfile(d, '*.txt')); len = length(files); for i = 1:len a = files(i).name; filename{i} = a; path = [d,'\',a]; colo

我正在尝试使用colormap为绘图上的线条指定颜色。每行的数据都是从文件生成的,每次导入的文件数/打印的行数都是可变的。我的代码是:

d = uigetdir(pwd, 'Select a folder');
files = dir(fullfile(d, '*.txt'));
len = length(files);
for i = 1:len
    a = files(i).name;
    filename{i} = a;
    path = [d,'\',a];
    colour = round(random('unif',0,200,1,3))/255;
    data = dlmread(path);
    plot(data(:,1), data(:,2),'color',colour,'linewidth',2);
    hold on;
end
hold off;
目前,线条的颜色是随机生成的,但我真的希望使用
colormap(jet(n))
使线条从红色延伸到蓝色,并在光谱中均匀分布


然而,由于每次导入的文件数量不同,我不知道n将是多少。我曾尝试在代码中使用colormap,但每次都会出错。

您可以指定要从colormap中使用的等距颜色数,例如,
jet(20)
将为您提供从蓝色到红色的20种等距RGB颜色

您可以使用此选项为各条线着色,如下所示:

x = [0:0.1:10];
linecolors = jet(5);
for i=1:5
    plot(x,x.^(i/3),'color',linecolors(i,:));
    hold on;
end

应用于您的特定问题,代码如下所示(未测试):

d= uigetdir(pwd, 'Select a folder');

files = dir(fullfile(d, '*.txt'));

len = length(files);

linecolors = jet(len);

for i = 1:len

    a = files(i).name;

    filename{i} = a;

    path = [d,'\',a];

    data = dlmread(path);

    plot(data(:,1), data(:,2),'color',linecolors(i,:),'linewidth',2);

    hold on;

end

hold off;