Can data values be used to colour lines in the plot() function? As is the case with the scatter function
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Darik
2012-8-28
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.)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!