How do I assign certain colours to different sections of a 3plot?

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?

 Accepted Answer

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 Comments

Thank you Walter Roberson,
Could you use this code with plot3? I have tried to use plot3 with this code i.e.
plot3(YourMatrix(:,2), YourMatrix(:,3), YourMatrix(:,4), LineSpec, YourMatrix(:,1));
colormap(cmap);
and I keep getting errors.
Thanks again for the help
No. plot3() is for lines. Each line must have the same color. The closest you could get would be to plot3() the line into place with a constant color, and also scatter3() colored markers.
Would there be a way to separate the data in sections but keep each section in the original series and then use plot3 on each section i.e if I had a matrix
99 x y z;
99 x y z;
99 x y z;
104 x y z;
104 x y z;
99 x y z;
etc
so some how get matlab to plot3 the first section of 99s then plot3 again the second section of 104s and continue on until the end of the matrix?
Yes, that could be done. However, what color should the lines be on the segment joining the 99 to 104 and so on?
The colour of the joining segment would be the colour of the previous segment. so the segment between 99 to 104 would be green. Thanks again for your help
HI Walter,
I have written the following script using the your color mapping to plot the data in sections of color but am getting errors. any suggestions?
for i=1:length(YourMatrix);
start=1;
if YourMatrix(i,1)~= YourMatrix(i+1,1)
plot3(YourMatrix(:,2),YourMatrix(:,3),YourMatrix(:,4),'color',YourMatrix(:,1));
colormap(cmap);
start=i+1;
end
end
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);

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!