How do I color lines by a third variable?

4 views (last 30 days)
Cole Messa
Cole Messa on 8 Apr 2022
Commented: Image Analyst on 8 Apr 2022
I would like to color 27 lines that are plotted from an array using a for loop by color according to a third variable as defined in its own array, but I cannot figure out how to get the coloring to work. All the lines are plotted but their colors are random, of course. Any help is greatly appreciated.
My code is below. I want to plot my 27 lines (which are in ciisoarr) and color them according to their value in eNdarr.
%Code for importing data from a 45N Data Excel Spreadsheet and defining
%arrays
ciiso = readtable("cinormisot.xlsx");
ciisoarr = table2array(ciiso);
eNd = readtable("eNdtable.xlsx");
eNdarr = table2array(eNd);
elements = {'La';'Ce';'Pr';'Nd';'Sm';'Eu';'Gd';'Tb';'Dy';'Ho';'Er';'Tm';'Yb';'Lu'};
elements = categorical(elements);
%Plot code below
for i = 1:27
plot(ciisoarr(:,i))
hold on
end
ylabel("CI-Normalized",'FontWeight','bold','FontSize',16)
ylim([1 100])
set(gca,'YScale','log')
set(gca,'xtick',[1:14],'xticklabel',elements,'FontWeight','bold','FontSize',12)
  3 Comments
Voss
Voss on 8 Apr 2022
Can you upload the .xlsx files?
Cole Messa
Cole Messa on 8 Apr 2022
I cannot. The plotted lines are 27 columns of data, each plotted as a line. The data that I want to color the lines by is a single column of 27 numbers.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 8 Apr 2022
Try this:
% Create data since you won't upload it.
ciisoarr = zeros(50, 27);
for col = 1 : 27
ciisoarr(:, col) = rand(50, 1) + col;
end
% Now we have data and we can make up a list of colors and plot it.
cmap = turbo(27);
for col = 1 : size(ciisoarr, 2)
plot(ciisoarr(:, col), '-', 'Color', cmap(col,:), 'LineWidth', 3);
hold on;
end
hold off;
grid on;
  2 Comments
Cole Messa
Cole Messa on 8 Apr 2022
Thanks! Now how do I make the line colors based off an array of 27 numbers?
Image Analyst
Image Analyst on 8 Apr 2022
Not sure what you mean. The colormap has 27 colors in it. The first column of cmap is the red value, the second value is the green value, and the third value is the blue value. You can create a list of colors in any way that you want. You can create it programmatically like I did or you can use the app:
>> colormapeditor
Not sure what your 27 element vector is. If you want, it could be pointers to colors (rows) in the colormap, like
for col = 1 : size(ciisoarr, 2)
% Find out what row of the colormap the vector is directing us to.
thisColorsRow = vec27(col);
% Extract the actual color from this row of the colormap.
thisColor = cmap(thisColorsRow, :); % A 1 by 3 vector of [r, g, b] values in the range 0 to 1.
% Plot the curve with this specific color
plot(ciisoarr(:, col), '-', 'Color', thisColor, 'LineWidth', 3);
hold on;
end
but I don't think that's really what you want, so you have to explain more.

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!