Clear Filters
Clear Filters

How can I set different colors for different lines in a plot?

3 views (last 30 days)
Hi,
I have a vector of values named result, that indicates the color intensity of each pixel on the same vertical line on a image. I want tp plot it now with a colorbar. For the colorbar I defined the colors needed in the matrix colors. I used scatter to plot each point of result with its corresponding color and I connected all point using line().
That works well. Now I want to draw vertical line for each point and each line has to have the color of the corresponding point.
How can I do it?
Here is my code:
%defining the colors
image = imread('color_deg.jpg'); % image containing all colors needed
size_image = size(image);
ln = size_image(1);
colors = zeros(ln, 3);
% set all color values
for i=1:ln
vector = squeeze(image(i,10,:));
colors(i,:) = vector;
end
x = 1:1:size_result(2); %vector who has the lenght of the y-axis [1 2 3 ... s(2)]
ss = 50;
Color1 = colors/255;
fig = figure
colormap(Color1);
h=scatter(x,result,ss,result,'filled')
line(x,result) %connect all points
%draw vertical lines
for i=1:s(2)
line([i i],[0 result(i)])
end
colorbar
caxis([0 170]);
Here is an example of what my program can do now. I just want to set the colors for the vertical lines
Unbenannt.png

Answers (1)

Kevin Phung
Kevin Phung on 31 Jan 2019
Example:
a = line(x,y) % a is the handle to my line object
a.Color = [1 0 0] %red
%or you can do:
set(a,'Color',[1 0 0])
  1 Comment
Ouael Chkoundali
Ouael Chkoundali on 31 Jan 2019
Edited: Ouael Chkoundali on 31 Jan 2019
Thanks for your answer Kevin. But the color that I need is saved in the matrix Color1. The problem is that I can't kwon from wich line of the matrix it is. Is there any possibility to get the color of the points from the scatter plot or from the colorbar?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!