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

採用された回答

Darik
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.)
  1 件のコメント
Mel
Mel 2012 年 8 月 28 日
Great it works! Really like the use of patch, very inventive.
Thank you for the help
Mel

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by