How to make colororder function working in scatter plot

5 Ansichten (letzte 30 Tage)
Avijit Paul
Avijit Paul am 2 Mär. 2025
Kommentiert: Voss am 3 Mär. 2025
I am trying to use the colororder function in my scatter plot, but somehow it is not working and couldn't able to understand why.
% Data file is attached
figure(1);
newcolors=["#FF0000" "#00FF00" "#0000FF" "#00FFFF" "#FF00FF"];
colororder(newcolors);
s=scatter(lon,lat,36,index(1,:),"filled");
ax = gca;
ax.FontSize = 12;
xticks([66 74 82 90 98]);
xticklabels([66 74 82 90 98]);
ylim([6.5 39.5]);
yticks([8 14 20 26 32 38]);
ylabel(ax,"Latitude","FontSize",20);
xlabel(ax,"Latitude","FontSize",20);

Akzeptierte Antwort

Voss
Voss am 2 Mär. 2025
Bearbeitet: Voss am 2 Mär. 2025
When you specify a vector of values for the colors (index(1,:) in this case), those values are interpreted as indices into the axes' Colormap, as explained here.
  • Vector of colormap indices — A vector of numeric values that is the same length as the x and y vectors.
Therefore you need to set the axes Colormap rather than its ColorOrder. See below for one way to do that.
load Data
% Data file is attached
figure(1);
newcolors=["#FF0000" "#00FF00" "#0000FF" "#00FFFF" "#FF00FF"];
s=scatter(lon,lat,36,index(1,:),"filled");
ax = gca;
ax.Colormap = hex2rgb(newcolors);
ax.FontSize = 12;
xticks([66 74 82 90 98]);
xticklabels([66 74 82 90 98]);
ylim([6.5 39.5]);
yticks([8 14 20 26 32 38]);
ylabel(ax,"Latitude","FontSize",20);
xlabel(ax,"Latitude","FontSize",20);
When no colors are specified in the scatter() call, then the resulting scatter objects are colored according to the axes ColorOrder, with one color per scatter object (not necessarily one color per scatter point). [The examples in the scatter documentation showing the effect of changing the axes ColorOrder create multiple scatter objects, letting the scatter function determine their colors automatically from the axes ColorOrder.] That is not relevant to this case, since your scatter object needs to have multiple colors in it, in order for its points to be colored according to index(1,:) and newcolors.
  3 Kommentare
Voss
Voss am 3 Mär. 2025
You're welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by