highlight Nodecolor for a graph using a color map (JET)

8 ビュー (過去 30 日間)
Sha
Sha 2020 年 9 月 30 日
コメント済み: Sha 2020 年 10 月 1 日
Hi,
I have plotted a graph with n nodes.
Each node has an "attribute" or a value from -1 to 1 stored in a vector K.
I would like to color the nodes using these attributes so for example if node 1 has attribute value "-1" then with the "JET" colormap on Matlab it would appear in Blue.
Is there a way to achieve this ?
I tried the following but clearly it doesn't work (returns error, it's wrong):
jetcustom=jet(n);
r1=K(1,:); %list of attributes
colors = interp1(linspace(-1, 1, n), jetcustom, r1.');
figure()
a=graph(A);
p=plot(a,'Layout','force');
for i=1:n
highlight(p,i , 'NodeColor', colors(i));
end

採用された回答

Christine Tobler
Christine Tobler 2020 年 9 月 30 日
Since colors is a n-by-3 array, use colors(i, :) to access the complete row that represents the color for node i. You can also use
p=plot(a,'Layout','force','NodeColor',colors)
instead of the for-loop with highlight, or you could try to have MATLAB's colormap work in the background without explicitly setting the colors:
p=plot(a,'Layout','force','NodeCData',r1);
colorbar
colormap jet
  1 件のコメント
Sha
Sha 2020 年 10 月 1 日
thanks! worked perfectley!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by