How do I assign certain colours to different sections of a 3plot?
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a 4 column matrix. The first column is assigned a number, 99,104 or 115 and the last three columns represents a position x,y,z in a 3D space. Each number in the first column represents a colour, 99-green, 104-red and 115-blue. How could I plot this 3D data with respect to the assigned colours?
댓글 수: 0
채택된 답변
Walter Roberson
2016년 4월 13일
cmap = zeros(115,3);
cmap(99,:) = [0 1 0]; %99 is green
cmap(104,:) = [1 0 0]; %104 is red
cmap(115,:) = [0 0 1]; %115 is blue
pointsize = 40;
scatter3(YourMatrix(:,2), YourMatrix(:,3), YourMatrix(:,4), pointsize, YourMatrix(:,1));
colormap(cmap);
caxis([1,115])
This makes use of the fact that your particular values are small positive integers, and uses them as indices into a colormap that has been created to have the desired colors at those values. Then it says to map "1" to the bottom color, "115" to the top color -- essentially mapping 1 to 1 for the 115 entry color map.
댓글 수: 7
Walter Roberson
2016년 4월 21일
편집: Walter Roberson
2016년 4월 21일
codes = [inf; YourMatrix(:,1); -inf]);
breakpoints = find(diff(codes));
breakpoints(end) = breakpoints(end)-1; %reaches one past end
for K = 1 : length(breakpoints)-1
startpos = breakpoints(K);
endpos = breakpoints(K+1);
plot3(YourMatrix(startpos:endpos, 2), YourMatrix(startpos:endpos, 3), YourMatrix(startpos:endpos, 4), 'color', YourMatrix(startpos,1) );
hold on
end
hold off
colormap(cmap);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Red에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!