How to make colororder function working in scatter plot

5 ビュー (過去 30 日間)
Avijit Paul
Avijit Paul 2025 年 3 月 2 日
コメント済み: Voss 2025 年 3 月 3 日
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);

採用された回答

Voss
Voss 2025 年 3 月 2 日
編集済み: Voss 2025 年 3 月 2 日
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 件のコメント
Avijit Paul
Avijit Paul 2025 年 3 月 3 日
Thanks a lot
Voss
Voss 2025 年 3 月 3 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeScatter Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by