how to color individual lines in a plot
1,719 views (last 30 days)
Show older comments
Josh Culpepper
on 4 Jan 2019
Commented: Josh Culpepper
on 9 Jan 2019
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 Comments
Accepted Answer
Image Analyst
on 4 Jan 2019
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.
More Answers (1)
Arvind Sathyanarayanan
on 4 Jan 2019
Edited: Arvind Sathyanarayanan
on 4 Jan 2019
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 Comments
Image Analyst
on 5 Jan 2019
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?
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)?
See Also
Categories
Find more on Colormaps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!