plot the lines with different color
2 views (last 30 days)
Show older comments
Dear all,
I have generated a plot using x and y and would like to add color on it based on its value (diameter of line). I have used the https://github.com/vistalab/vistateach/blob/master/cogneuro/tutorial1_timeseries/vals2colormap.m
to convert my range of value to rgb value
x = [0.168149323 0.547990492 0.580721962 0.502272216 0.321295719 0.168149323];
y = [0.565541486 0.530969047 0.570321397 0.878624621 1.069086578 0.565541486];
colorrange=[27.65 22.11 23.11 25.11 26.74 26.94]
rgb=vals2colormap(colorrange, 'jet',[22.11 27.65])
%rgb=[0.5,0,0;0,0,0.515625;0,0.234375,1;0.671875,1,0.3281250;1,0.15625,0;1,0.015625,0]
%rgb is colormap value for different line
for k = 1 : length(rgb)
hold on
% Get new values.
plot(x, y,'-', 'Color', rgb(k,:) ,'LineWidth',2)
end
however, when I plot them out, all the color are the same.

any idea I can make different line to different color based on their specific value?
0 Comments
Accepted Answer
Image Analyst
on 6 Jun 2022
Try this:
x = [0.168149323 0.547990492 0.580721962 0.502272216 0.321295719 0.168149323];
y = [0.565541486 0.530969047 0.570321397 0.878624621 1.069086578 0.565541486];
numColors = numel(x)
cmap = jet(numColors);
colorrange=[27.65 22.11 23.11 25.11 26.74 26.94]
cMapIndexes = round(rescale(colorrange, 1, numColors))
for k = 2 : length(x)
hold on
% Get new values.
thisColor = cmap(cMapIndexes(k),:);
fprintf('Plotting line from point %d to point %d as [%.2f, %.2f, %.2f].\n', ...
k-1, k, thisColor(1), thisColor(2), thisColor(3))
plot([x(k-1), x(k)], [y(k-1), y(k)], '-', 'Color', thisColor ,'LineWidth', 5)
end
grid on;
xlabel('x');
ylabel('y')
More Answers (0)
See Also
Categories
Find more on Color and Styling 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!