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
Voss
Voss 2025년 3월 3일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by