- Vector of colormap indices — A vector of numeric values that is the same length as the x and y vectors.
How to make colororder function working in scatter plot
4 views (last 30 days)
Show older comments
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);
0 Comments
Accepted Answer
Voss
on 2 Mar 2025
Edited: Voss
on 2 Mar 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.
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 Comments
More Answers (0)
See Also
Categories
Find more on Scatter Plots 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!