在 Networkx 中重命名图形
Renaming a Graph in Networkx
我是 Python 的新手,我正在尝试学习 Networkx ( https://networkx.github.io/ )
我正在尝试 运行 一个基本的代码:
import networkx as nx
import matplotlib.pyplot as plt
G=nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_node(4)
G.add_node(5)
G.add_node(6)
G.add_node(7)
G.add_edge(1,2)
G.add_edge(2,3)
G.add_edge(3,4)
G.add_edge(4,5)
G.add_edge(5,6)
G.add_edge(6,7)
G.add_edge(7,1)
G.add_edge(1,3)
G.add_edge(1,4)
G.add_edge(1,5)
G.add_edge(1,6)
G.add_edge(1,7)
G.add_edge(2,4)
G.add_edge(2,5)
G.add_edge(2,6)
G.add_edge(2,7)
G.add_edge(3,5)
G.add_edge(3,6)
G.add_edge(3,7)
G.add_edge(4,6)
G.add_edge(4,7)
G.add_edge(5,7)
nx.draw(G)
plt.savefig("graph1.png")
plt.show()
这是生成的图表:
尝试向节点添加名称时出现问题。我正在 运行 下一个代码:
import networkx as nx
import matplotlib.pyplot as plt
G=nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_node(4)
G.add_node(5)
G.add_node(6)
G.add_node(7)
G.add_edge(1,2)
G.add_edge(2,3)
G.add_edge(3,4)
G.add_edge(4,5)
G.add_edge(5,6)
G.add_edge(6,7)
G.add_edge(7,1)
G.add_edge(1,3)
G.add_edge(1,4)
G.add_edge(1,5)
G.add_edge(1,6)
G.add_edge(1,7)
G.add_edge(2,4)
G.add_edge(2,5)
G.add_edge(2,6)
G.add_edge(2,7)
G.add_edge(3,5)
G.add_edge(3,6)
G.add_edge(3,7)
G.add_edge(4,6)
G.add_edge(4,7)
G.add_edge(5,7)
names = {1:"Central",2:"South",3:"North",4:"East",5:"West",6:"Up",7:"Down"}
H=nx.relabel_nodes(G,names)
nx.draw(H)
plt.savefig("graph1.png")
plt.show()
结果图是这个:
如何为节点添加名称?我正在使用 python 3.8 和 Networkx 2.4
您可以使用 nx.draw(H, with_labels=True)
,
或 nx.draw_networkx(H)
,默认为 with_labels=True
。
draw(G, pos=None, ax=None, **kwds)
[...]
kwds (optional keywords) – See networkx.draw_networkx() for a description of optional keywords.
documentation of nx.draw_networkx
draw_networkx(G, pos=None, arrows=True, with_labels=True, **kwds)
[...]
with_labels (bool, optional (default=True)) – Set to True to draw labels on the nodes.
编辑:
绘制不同颜色的边:
相关属性:
edge_color (color string, or array of floats) – Edge color. Can be a single color
format string (default=’r’), or a sequence of colors with the same length as edgelist. If numeric values are specified they will be mapped to colors using the edge_cmap and edge_vmin,edge_vmax parameters.
style (string) – Edge line style (default=’solid’) (solid|dashed|dotted,dashdot)
alpha (float) – The edge transparency (default=1.0)
cmap (edge) – Colormap for mapping intensities of edges (default=None)
edge_vmin,edge_vmax (floats) – Minimum and maximum for edge colormap scaling (default=None)
因此,您可以创建一个字符串列表:
colors = ['red'] * len(G.edges()
pos = nx.spring(layout(G))
nx.draw_networkx_nodes(G, pos=pos)
nx.draw_networkx_edges(G, pos=pos, edge_color=colors)
或使用数字和颜色图:
colors = [np.random.rand() for e in G.edges()]
pos = nx.spring(layout(G))
nx.draw_networkx_nodes(G, pos=pos)
nx.draw_networkx_edges(G, pos=pos, edge_color=colors, cmap='viridis')
我是 Python 的新手,我正在尝试学习 Networkx ( https://networkx.github.io/ )
我正在尝试 运行 一个基本的代码:
import networkx as nx
import matplotlib.pyplot as plt
G=nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_node(4)
G.add_node(5)
G.add_node(6)
G.add_node(7)
G.add_edge(1,2)
G.add_edge(2,3)
G.add_edge(3,4)
G.add_edge(4,5)
G.add_edge(5,6)
G.add_edge(6,7)
G.add_edge(7,1)
G.add_edge(1,3)
G.add_edge(1,4)
G.add_edge(1,5)
G.add_edge(1,6)
G.add_edge(1,7)
G.add_edge(2,4)
G.add_edge(2,5)
G.add_edge(2,6)
G.add_edge(2,7)
G.add_edge(3,5)
G.add_edge(3,6)
G.add_edge(3,7)
G.add_edge(4,6)
G.add_edge(4,7)
G.add_edge(5,7)
nx.draw(G)
plt.savefig("graph1.png")
plt.show()
这是生成的图表:
尝试向节点添加名称时出现问题。我正在 运行 下一个代码:
import networkx as nx
import matplotlib.pyplot as plt
G=nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_node(4)
G.add_node(5)
G.add_node(6)
G.add_node(7)
G.add_edge(1,2)
G.add_edge(2,3)
G.add_edge(3,4)
G.add_edge(4,5)
G.add_edge(5,6)
G.add_edge(6,7)
G.add_edge(7,1)
G.add_edge(1,3)
G.add_edge(1,4)
G.add_edge(1,5)
G.add_edge(1,6)
G.add_edge(1,7)
G.add_edge(2,4)
G.add_edge(2,5)
G.add_edge(2,6)
G.add_edge(2,7)
G.add_edge(3,5)
G.add_edge(3,6)
G.add_edge(3,7)
G.add_edge(4,6)
G.add_edge(4,7)
G.add_edge(5,7)
names = {1:"Central",2:"South",3:"North",4:"East",5:"West",6:"Up",7:"Down"}
H=nx.relabel_nodes(G,names)
nx.draw(H)
plt.savefig("graph1.png")
plt.show()
结果图是这个:
如何为节点添加名称?我正在使用 python 3.8 和 Networkx 2.4
您可以使用 nx.draw(H, with_labels=True)
,
或 nx.draw_networkx(H)
,默认为 with_labels=True
。
draw(G, pos=None, ax=None, **kwds)
[...]
kwds (optional keywords) – See networkx.draw_networkx() for a description of optional keywords.
documentation of nx.draw_networkx
draw_networkx(G, pos=None, arrows=True, with_labels=True, **kwds)
[...]
with_labels (bool, optional (default=True)) – Set to True to draw labels on the nodes.
编辑:
绘制不同颜色的边:
相关属性:
edge_color (color string, or array of floats) – Edge color. Can be a single color
format string (default=’r’), or a sequence of colors with the same length as edgelist. If numeric values are specified they will be mapped to colors using the edge_cmap and edge_vmin,edge_vmax parameters.
style (string) – Edge line style (default=’solid’) (solid|dashed|dotted,dashdot)
alpha (float) – The edge transparency (default=1.0)
cmap (edge) – Colormap for mapping intensities of edges (default=None)
edge_vmin,edge_vmax (floats) – Minimum and maximum for edge colormap scaling (default=None)
因此,您可以创建一个字符串列表:
colors = ['red'] * len(G.edges()
pos = nx.spring(layout(G))
nx.draw_networkx_nodes(G, pos=pos)
nx.draw_networkx_edges(G, pos=pos, edge_color=colors)
或使用数字和颜色图:
colors = [np.random.rand() for e in G.edges()]
pos = nx.spring(layout(G))
nx.draw_networkx_nodes(G, pos=pos)
nx.draw_networkx_edges(G, pos=pos, edge_color=colors, cmap='viridis')