Color text based on a variable

I am trying to make a plot of 3 properties of chemical elements. I would like the first property on the x-axis, the second on the y-axis, and the third presented by the color of the plot points. Using 'scatter' to do this is trivial, e.g.:
x=1:10;
y=1:10;
c=1:10;
figure
xlim([0 11]);ylim([0 11])
scatter(x,y,50,c,'.')
colorbar
However, I would like the graph to plot the abbreviation for the chemical in question, rather than a standard matlab symbol...
I can do this using the 'text' command:
x=1:10;
y=1:10;
chem={'H','He','Li','Be','B','C','N','O','F','Ne'};
figure
xlim([0 11]);ylim([0 11])
text(x,y,chem)
But I want to color the text labels to correspond to a colorbar representing my third parameter, as they are in the scatter plot.
I cannot find a way to do this - any ideas?
Thanks in advance!

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 8월 16일

0 개 추천

x=1:10;
y=1:10;
c=1:10;
chem={'H','He','Li','Be','B','C','N','O','F','Ne'};
h=figure(1);
xlim([0 11]);ylim([0 11]);
scatter(x,y,50,c,'.')
colorbar;
col=get(h,'colormap');
h_text=text(x,y,chem);
for k=1:length(h_text)
set(h_text(k),'color',col(k*6,:))
end

댓글 수: 3

Oscar Branson
Oscar Branson 2011년 8월 16일
Thanks!
Oscar Branson
Oscar Branson 2011년 8월 16일
P.S. For anyone looking at this later on and wondering why there's a '*6' in the line:
set(h_text(k),'color','col(k*6,:))
it's because the colormap is a 64 row array of 3-column RGB values. As we have 10 labels here, multiplying k by 6 will evenly space the colors across the colormap. A more adaptable code is:
lcol=size(colormap,1);
for k=1:length(h_text)
set(h_text(k),'color',col(k*round(c(k)*lcol/max(c)),:))
end
Fangjun Jiang
Fangjun Jiang 2011년 8월 16일
Nice add-on!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Color and Styling에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by