1
votes

I have some vectors I am plotting in a scatter plot: x, y, and z.

I plot these vectors using the scatter3 function.

scatter3(x, y, z)

This now provides me with my scatterplot correctly. However, all points are blue, and I need to colour some of the points different colours.

To this end I have a vector 'colours'. Colours is the same length as x, y, and z. Colours is a character array, consisting of the different characters for colours in matlab, such as 'b' for blue, 'r' for red', and so on.

//A small example: colours == 'bbbbbyyyrrr'

I realise I can probably loop through all points and recolor after plotting. However, the simulations produce large vectors, and this is very inefficient with time.

Is there a way to use the colours vector to colour the points such that the point given by x(3), y(3), z(3), would be of colour colours(3)?

1
Use scatter3(x,y,z,[],c) where c is a vector where, for example, 1 corresponds to 'b', 2 to 'c', etc. then set up a custom colormap for the colours. c can also be an n by 3 matrix of RGB values for each data point. - David
@David What do you mean by setting up a custom colormap? I read the colormap documentation with the example mymap = [0 0 0 1 0 0 0 1 0 0 0 1 1 1 1]; I understand these are the rgb triplets, and also that I can convert from char to rgb triplet. However, to set up a custom colormap I assume you call colormap(mymap) -- how does this affect the scatter function when it's taking chars as an input? Without setting up a custom colormap, I get the error "Error in color/linetype argument". - Shawn

1 Answers

0
votes

Build a Map object to map characters to rgb values (based on this table), then given a vector of characters you can read the rgb values from the map:

cmap = containers.Map({'y','m','c','r','g','b','w','k'},{[1 1 0],[1 0 1],[0 1 1],[1 0 0],[0 1 0],[0 0 1],[1 1 1],[0 0 0]});
cols = 'bbbbbyyyrrr';
rgb_vals = cell2mat(values(cmap,cellstr(cols(:))));
scatter3(1:11,1:11,1:11,[],rgb_vals)

The values function gets the values of the specified keys, and cell2mat converts the cell array which is the output of values back to a matrix which scatter3 can handle.