# Setting scatter point color when scattering matrices instead of vectors.

24 views (last 30 days)
Abel Abraham on 28 Jan 2023
Commented: dpb on 28 Jan 2023
I am doing a scatter plot with two matrices with each row being a custom color based on a colormap.
scatter(x,y) works fine, x and y are both 3982x100, but my color vector is 3982x1.
scatter(x,y,[],c) gives me an error
Error using scatter (line 61)
Color must be one RGB triplet, an m-by-3 matrix of RGB triplets with one color per scatter point, or an
m-by-1 vector with one value per scatter point.
Convering my color vector c into a 3982x100 matrix doesn't work and gives the same error. Using reshape to turn a 3982x100 color matrix into a 398200 column vector also doesn't work even though it is an m by 1 vector with a value for each of my scatter points.
How can I make each row it's own color? I really don't want to make a for loop since it is 3982 separate plots to make.
EDIT: I realize now that I can reshape the input matrices, but I feel like there should still be a better way to do this.
The simplified question is
scatter([1,2;3,4],[1,2;3,4],[],[5;5;5;5])
Error using scatter
Color must be one RGB triplet, an m-by-3 matrix of RGB triplets with one color per scatter point, or an m-by-1 vector with one value per scatter point.
why doesn't this work? I have 4 scatter points and a 4 element color vector.

dpb on 28 Jan 2023
Edited: dpb on 28 Jan 2023
Read the <scatter> doc on the color input options carefully -- the individual color by point works only if each x,y are vectors, not matrices.
If the x,y are matrices, then scatter treats each column as a SET of points and color applies to the set, NOT each point, and for this option, the color spec can ONLY be a set of RGB triplets...
c = [1 0 0; 0.6 0 1];
hSC=scatter([1,2;3,4],[1,2;3,4],[],c);
You'll note the colors alternate from x=1,3 and x=2,4 as associated with the columns of the input array.
It's confusing at best, granted, but the scatter object returned is
hSC
hSC =
1×2 Scatter array: Scatter Scatter
a two-vector object array, NOT four; scatter does NOT create a scatter object for each point this way, but only one for each column.
To color by row would mean transposing the input array so rows become columns; but you'll have to define/use the RGB triplets, not the colormap index.
The error message could be more explicit as to what the actual failure was, though, granted; looks like it's still left over from before could use other than vectors for inputs.
##### 2 CommentsShow 1 older commentHide 1 older comment
dpb on 28 Jan 2023
If you build the colormap or use a section of a builtin one, then there's not much of an issue about passing it with an indexing vector into it based on the data.
Why scatter can't do this for you automagically has always seemed rather rude to not accomdate, but it's never percolated to the top of the requested enhancements pile to be added.