Can data values be used to colour lines in the plot() function? As is the case with the scatter function
조회 수: 3 (최근 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에서 Scatter Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!