how to choose different colors for nodes

88 ビュー (過去 30 日間)
Kashfia Mahin
Kashfia Mahin 2019 年 8 月 25 日
コメント済み: Kashfia Mahin 2019 年 8 月 27 日
t = [1 1 1 1];
h = [2 3 4 5];
g = graph(t,h);
p = plot(g);
g.Edges.value = [10; 20; 20; 100];
g.Nodes.value = [0.3; 0.564; 12; 1; 0.005];
g.Nodes.NodeColors = g.Nodes.value;
g.Edges.EdgeColors = g.Edges.value;
p.NodeCData = g.Nodes.NodeColors;
p.EdgeCData = g.Edges.EdgeColors;
q= plot(g,'NodeCData',g.Nodes.NodeColors);
colorbar
t is the source node as input and h is demand node as output. I want to two different colors for input and output. But I don't find correct way. Plz help me to solve it.
  3 件のコメント
Adam Danz
Adam Danz 2019 年 8 月 25 日
"I want to two different colors for input and output. But I don't find correct way."
Does that mean you want the point labeled "1" to be one color and all the other points should be a different color?
Guillaume
Guillaume 2019 年 8 月 25 日
@darova, scatter does not have an overload for graph objects. Only plot has.
@Kashfia, it seems you already know how to set the colour of the nodes using 'NodeCData' (although the syntax you use is undocumented, it's equivalent to 'NodeCData', 'flat'), so like Adam, I'm not sure what you're asking.

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

採用された回答

Steven Lord
Steven Lord 2019 年 8 月 26 日
When you call plot on your graph or digraph object, specify the 'NodeColor' property. For example, here are two ways to create a digraph like the one in your example and plot it with node 1 being red and the others being green. I put each in their own figure window so you can compare the results.
d = digraph;
d = addedge(d, 1, 2:5);
You can explicitly specify a numnodes(d)-by-3 matrix of RGB values:
figure
p = plot(d, 'NodeColor', [1 0 0; 0 1 0; 0 1 0; 0 1 0; 0 1 0]);
Or you can plot with all the nodes being the same color and highlight ones that are in some way 'special'.
figure;
h = plot(d, 'NodeColor', 'g');
highlight(h, 1, 'NodeColor', 'r');
  1 件のコメント
Kashfia Mahin
Kashfia Mahin 2019 年 8 月 27 日
Thanks Mr. Steven

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by