plot the lines with different color

2 views (last 30 days)
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?

Accepted Answer

Image Analyst
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)
numColors = 6
cmap = jet(numColors);
colorrange=[27.65 22.11 23.11 25.11 26.74 26.94]
colorrange = 1×6
27.6500 22.1100 23.1100 25.1100 26.7400 26.9400
cMapIndexes = round(rescale(colorrange, 1, numColors))
cMapIndexes = 1×6
6 1 2 4 5 5
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
Plotting line from point 1 to point 2 as [0.00, 0.00, 1.00]. Plotting line from point 2 to point 3 as [0.00, 0.50, 1.00]. Plotting line from point 3 to point 4 as [0.50, 1.00, 0.50]. Plotting line from point 4 to point 5 as [1.00, 1.00, 0.00]. Plotting line from point 5 to point 6 as [1.00, 1.00, 0.00].
grid on;
xlabel('x');
ylabel('y')
  1 Comment
wei ong
wei ong on 6 Jun 2022
Thanks @Image Analyst, the rescale tool works like a cham. It took me a while to understand its function. really appreicate your help !

Sign in to comment.

More Answers (0)

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!