Matlab颜色图线图 [英] Matlab colormap line plot
问题描述
我正在尝试使用颜色图将颜色分配给绘图上的线.每行的数据是从一个文件生成的,每次导入的文件数/绘制的行数都是可变的.我的代码是:
I'm trying to use colormap to assign colors to lines on a plot. The data for each line is generated from a file, and the number of files imported/ lines plotted are variable each time. My code for this is:
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))
,以便线条从红色到蓝色,并且在光谱上等距分布.
At the moment the colors of the lines are generated randomly, but I would really like to use colormap (jet(n))
so that the lines run from red to blue and are equally spaced in the spectrum.
但是,由于每次要导入不同数量的文件,所以我不知道n会是多少.我曾尝试在代码中使用colormap,但每次都会出错.
However, as a different number of files are being imported each time, I don't know how much n will be. I have tried working colormap into my code, but I get errors each time.
推荐答案
您可以从颜色表中指定所需的等距颜色数,例如jet(20)
将为您提供20种等距的RGB颜色,从蓝色到红色.
You can specify the number of equally spaced colors you want from a colormap, so e.g. jet(20)
will give you 20 equally spaced RGB colors from blue to red.
您可以使用此方法为您的各个行着色,如下所示:
You can use this to color your individual lines like this:
x = [0:0.1:10];
linecolors = jet(5);
for i=1:5
plot(x,x.^(i/3),'color',linecolors(i,:));
hold on;
end
适用于您的特定问题,代码看起来像这样(未经测试):
Applied to your specific problem, the code looks something like this (untested):
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;
这篇关于Matlab颜色图线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!