how to color individual lines in a plot

398 views (last 30 days)
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.

Accepted Answer

Image Analyst
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.
0000 Screenshot.png
  1 Comment
Josh Culpepper
Josh Culpepper on 9 Jan 2019
Hi Image Analyst,
Your code looks like what I want to do exactly; however, I'm having trouble getting the code to work (again, pretty new to Matlab). You can see from the inital code that I posted that I'm trying to plot "midnighttemps". This variable is a 31x25 double. So it plots 25 lines of length 31.
I haven't figured out a way to implement your code on the plotted variable. Any suggestions? I'm going to try to keep working with your code because it's exactly what I wanted. Here's an example of what I've tried:
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
newDefaultColors = jet(25)
%subplot(1,2,1);
set(gca, 'ColorOrder', newDefaultColors)
plot(midnighttemps);
view([90 -90]);
title('midnight');
However, this is still not working. Thank you and thank you for any additional suggestions.

Sign in to comment.

More Answers (1)

Arvind Sathyanarayanan
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.
The documentation some more info in it.
%Using color names
plot(midnighttemps, 'color', 'red');
%Using RGB code
plot(noontemps, 'color', [1 1 0.8]);
If you want vary the color of the line with time, please see this MATLAB Answers post.
  4 Comments
Image Analyst
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? 0000 Screenshot.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)?
Josh Culpepper
Josh Culpepper on 7 Jan 2019
Edited: Josh Culpepper on 7 Jan 2019
Hi Image Analyst,
Your code looks like what I want to do exactly; however, I'm having trouble getting the code to work (again, pretty new to Matlab). You can see from the inital code that I posted that I'm trying to plot "midnighttemps". This variable is a 31x25 double. So it plots 25 lines of length 31.
I haven't figured out a way to implement your code on the plotted variable. Any suggestions? I'm going to try to keep working with your code because it's exactly what I wanted. Here's an example of what I've tried:
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
newDefaultColors = jet(25)
%subplot(1,2,1);
set(gca, 'ColorOrder', newDefaultColors)
plot(midnighttemps);
view([90 -90]);
title('midnight');
However, this is still not working. Thank you and thank you for any additional suggestions.

Sign in to comment.

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!