![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200187/0000%20Screenshot.png)
how to color individual lines in a plot
1,746 次查看(过去 30 天)
显示 更早的评论
Hello all,
I'm a novice user and I'm attempting to use something like "colormap jet" on a plot so that each line within the plot is a different color. The x-axis is temperature and the y-axis is depth. Each line is at a temperature, so I'd like the colors to make the graph easier to read (e.g. colder to hotter).
I haven't found a solution on here that works so far. I think it might have to do with how the data are plotted.
Here's the code:
dates = datevec(datetime); %set matlab dates to year,month,day,hour,min,sec
dates = dates'; %invert
temp_crop = calTemp(387:417,:); %only interested in temps along this length of cable
ind_midnight = find(dates(4,:)==0); %finding all temp measures along cable where hour is 0 (midnight)
ind_noon = find(dates(4,:)==12); %same as above, but with noon
midnighttemps = temp_crop(:,ind_midnight); %have all temps along cropped section of cable for each day at midnight
noontemps = temp_crop(:,ind_noon); %same as above, but with noon
close all
figure;
subplot(1,2,1);
plot(midnighttemps);
view([90 -90]);
title('midnight');
subplot(1,2,2);
plot(noontemps);
view([90 -90]);
title('noon');
You can see how the plot is actually plotting individual matrices with a single column. I think this might be why I can't do a simple colormap or colorbar. I've also tried a few for loops such as this:
x = [0:0.1:10];
linecolors = jet(5);
for i=1:5
plot(x,x.^(i/3),'color',linecolors(i,:));
hold on;
end
I found this on StackOverflow, and the graph it produces is what I'd like, individually colored lines on a gradient, but I can't seem to get it to work. Again, I think this is because I'm plotting an entire matrix. Any suggestions?
Thank you for any assistance.
0 个评论
采纳的回答
Image Analyst
2019-1-4
You can change the default color order to do exactly what you want. In fact I already have a demo that does it. See attached m-file.
![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200187/0000%20Screenshot.png)
更多回答(1 个)
Arvind Sathyanarayanan
2019-1-4
编辑:Arvind Sathyanarayanan
2019-1-4
Josh,
You can specify plot color by using the 'color' property and specifying a color or a normalized RGB code when using the plot command. Please see the example below.
%Using color names
plot(midnighttemps, 'color', 'red');
%Using RGB code
plot(noontemps, 'color', [1 1 0.8]);
4 个评论
Image Analyst
2019-1-5
Josh:
take a look at my answer above again. Look at the screenshot I posted. It seems to me it looks like the one below that your posted sample code makes, don't you think? ![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200199/0000%20Screenshot.png)
![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200199/0000%20Screenshot.png)
If you ran the code, you'll see it asks you to pick one of several colormaps and it does give you a gradient fo colors as you go from one line to the other. Did you actually run the code or not? Or you can tell just from looking at it that you don't want it, or the one above from StackOverflow? Do you instead want one where the color is not different for each line (each line has one color for the entire line), but maybe you want one where the color of each line actually changes as it goes from left to right (possible, but this is not what we though you wanted)?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!