How do I color lines by a third variable?

조회 수: 7 (최근 30일)
Cole Messa
Cole Messa 2022년 4월 8일
댓글: Image Analyst 2022년 4월 8일
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
Voss
Voss 2022년 4월 8일
Can you upload the .xlsx files?
Cole Messa
Cole Messa 2022년 4월 8일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2022년 4월 8일
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
Cole Messa
Cole Messa 2022년 4월 8일
Thanks! Now how do I make the line colors based off an array of 27 numbers?
Image Analyst
Image Analyst 2022년 4월 8일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Orange에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by