Can data values be used to colour lines in the plot() function? As is the case with the scatter function

1 view (last 30 days)
I have a series of (x,y) (x1,y1) point pairs with an associated attribute numerical value A.
I would like to plot each pair of points and colour the line joining them so the colour is representative of the size of A.
I know attributes can be used in the scatter() function to colour points http://www.mathworks.co.uk/help/techdoc/ref/scatter.html
Can this be done to colour the lines in plot graphs? I am assuming here that I will have to plot each pair of points separately, probably using a simple plot() function and hold on command, then loop through each pair of points. I would like to colour the lines to represent the entire range of A, can I integrate this into the plot command or should I setup a vector of colour specifiers relating to A?
Many Thanks
Mel

Accepted Answer

Darik
Darik on 28 Aug 2012
It might be easier to use a patch object directly instead of the plot function, although the syntax might seem a little bizarre.
x1 = rand(10,1); %Column vectors
y1 = rand(10,1);
x2 = rand(10,1);
y2 = rand(10,1);
A = rand(10, 1);
%Each line has two endpoints (vertices) and we need a color for each vertex so double up the color vector
AA = [A A]';
%Create the patch object. Each column in the X and Y arrays corresponds to one edge so the X and Y arrays should be a 2xNLINES array, i.e. X = [x1 x2]'
h = patch([x1 x2]', [y1 y2]', 1, 'CData', AA(:), 'EdgeColor', 'flat');
Then you can use the standard colormap function and axes CLim properties to change how color scales with A. (By default, the colormap is scaled to the whole range of A.)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!