NetworkX 显示错误的颜色
NetworkX shows wrong colors
我正在尝试使用颜色绘制带有 networkx 的简单网络,但是我为节点提供的颜色未显示在图表上...
import networkx as nx
G = nx.Graph()
G.add_nodes_from([
(4, {"color": "red"}),
(5, {"color": "green"}),
])
nx.draw_networkx(G)
绘制此图:
为什么这些节点不是我分配给它们的颜色?
我正在尝试重现 https://networkx.org/documentation/stable/tutorial.html
中的示例
文档中的示例具有误导性 - 它甚至不显示带有颜色的图像。
值{"color": "red"}
不用于改变颜色,只是在节点中保留额外的信息。您可以使用 {"first name": "James", "last name": "Bond", "number": "007"}
得到相同的结果
要设置颜色,您必须在绘制它们时使用列表
nx.draw(G, node_color=['red', 'green', 'blue'], with_labels=True)
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_nodes_from([
(4, {"color": "red"}),
(5, {"color": "green"}),
(7, {"first name": "James", "last name": "Bond", "number": "007"}),
])
nx.draw(G, node_color=['red', 'green', 'blue'], with_labels=True)
plt.show()
或者您将不得不使用 for
-loop 来使用来自节点
的数据创建列表
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_nodes_from([
1,
2,
(4, {"color": "red"}),
(5, {"color": "green"}),
(7, {"first name": "James", "last name": "Bond", "number": "007"}),
8,
9,
('Hello', {"color": "red"}),
])
#color_map = ['red', 'green', 'blue']
color_map = []
for node in G:
print(G.nodes[node])
if 'color' in G.nodes[node]:
color_map.append( G.nodes[node]['color'] )
else:
color_map.append('blue')
nx.draw(G, node_color=color_map, with_labels=True)
plt.show()
尝试:
import networkx as nx
G = nx.Graph()
G.add_nodes_from([
(4, {"color": "red"}),
(5, {"color": "green"}),
])
nx.draw_networkx(G, node_color=[i['color'] for i in dict(G.nodes(data=True)).values()])
输出:
我正在尝试使用颜色绘制带有 networkx 的简单网络,但是我为节点提供的颜色未显示在图表上...
import networkx as nx
G = nx.Graph()
G.add_nodes_from([
(4, {"color": "red"}),
(5, {"color": "green"}),
])
nx.draw_networkx(G)
绘制此图:
为什么这些节点不是我分配给它们的颜色?
我正在尝试重现 https://networkx.org/documentation/stable/tutorial.html
中的示例文档中的示例具有误导性 - 它甚至不显示带有颜色的图像。
值{"color": "red"}
不用于改变颜色,只是在节点中保留额外的信息。您可以使用 {"first name": "James", "last name": "Bond", "number": "007"}
得到相同的结果
要设置颜色,您必须在绘制它们时使用列表
nx.draw(G, node_color=['red', 'green', 'blue'], with_labels=True)
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_nodes_from([
(4, {"color": "red"}),
(5, {"color": "green"}),
(7, {"first name": "James", "last name": "Bond", "number": "007"}),
])
nx.draw(G, node_color=['red', 'green', 'blue'], with_labels=True)
plt.show()
或者您将不得不使用 for
-loop 来使用来自节点
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_nodes_from([
1,
2,
(4, {"color": "red"}),
(5, {"color": "green"}),
(7, {"first name": "James", "last name": "Bond", "number": "007"}),
8,
9,
('Hello', {"color": "red"}),
])
#color_map = ['red', 'green', 'blue']
color_map = []
for node in G:
print(G.nodes[node])
if 'color' in G.nodes[node]:
color_map.append( G.nodes[node]['color'] )
else:
color_map.append('blue')
nx.draw(G, node_color=color_map, with_labels=True)
plt.show()
尝试:
import networkx as nx
G = nx.Graph()
G.add_nodes_from([
(4, {"color": "red"}),
(5, {"color": "green"}),
])
nx.draw_networkx(G, node_color=[i['color'] for i in dict(G.nodes(data=True)).values()])
输出: