Using scatter with a custom colorbar

I am trying to create a scatter plot, and I want to use a 128x3 colorbar I wrote myself (1st dimension is # of triplets, 2nd dimension is each triplet).
Here is the command I am using:
scatter(lat,lon,12,obs,'filled',[],c)
where lat, lon, and obs are 6251x1 and c is the colorbar (128x3).
The error I keep returning is "Color must be one RGB triplet, an m-by-3 matrix of RGB triplets iwth one color per scatter point, or an m-by-1 vector with one value per scatter point.

2 comentarios

dpb
dpb el 5 de Mzo. de 2025
Editada: dpb el 5 de Mzo. de 2025
@Voss already answered how to use a custom colorbar but note that to color each point per the above syntax the color triplet array much match the number of elements in the x-, y- vectors. See scatter Input Arguments section for c, the marker color table for details.
As I noted in the other thread from some time ago, it's confusing, and even more so as in that thread when one uses a matrix x-, y- arrangement which scatter then treats each column as a dataset instead of each row or element so the number must match the number of columns instead.
KCE
KCE el 5 de Mzo. de 2025
I had tried some things from the scatter page previously and was also confused.

Iniciar sesión para comentar.

 Respuesta aceptada

Voss
Voss el 5 de Mzo. de 2025
scatter(lat,lon,12,obs,'filled')
colormap(c)

6 comentarios

KCE
KCE el 5 de Mzo. de 2025
This returns the same error as before, unfortunately.
What error?
N = 6251;
lat = rand(N,1);
lon = rand(N,1);
obs = rand(N,1);
c = jet(128);
scatter(lat,lon,12,obs,'filled')
colormap(c)
dpb
dpb el 5 de Mzo. de 2025
@KCE -- we need to see the code and error in context as well as the definitions of all the variables -- as @Walter Roberson shows, the syntax works as expected if the data are correct and there's no typo or other mistake in the command.
The command you show originally is invalid syntax; it doesn't match any of the shown forms in the scatter doc
KCE
KCE el 5 de Mzo. de 2025
I had initially changed my code to (try to) reflect the scatter doc, which is incorrect. I had an issue with one of the arrays used in the scatter command and it works now. Thank you for your help.
Voss
Voss el 5 de Mzo. de 2025
You're welcome!
dpb
dpb el 6 de Mzo. de 2025
@KCE wrote "to) reflect the scatter doc, which is incorrect."
What, specifically, do you think is incorrect in the documentation for scatter? I have seen nothing that is incorrect albeit one does have to study it in some detail to get the differences in behavior depending upon the shape of the input arrays.
If there actually is some error, it should be brought to Mathworks' attention so it can be corrected.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Color and Styling en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

KCE
el 5 de Mzo. de 2025

Comentada:

dpb
el 6 de Mzo. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by