And here is what my plot looks like as of now.
How do I color lines by a third variable?
4 views (last 30 days)
Show older comments
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
Answers (1)
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
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.
See Also
Categories
Find more on Orange 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!