Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在Networkx中重命名图形_Python 3.x_Networkx_Graph Theory - Fatal编程技术网

Python 3.x 在Networkx中重命名图形

Python 3.x 在Networkx中重命名图形,python-3.x,networkx,graph-theory,Python 3.x,Networkx,Graph Theory,我是Python新手,我正在努力学习Networkx() 我正在尝试运行基本a代码: 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_e

我是Python新手,我正在努力学习Networkx()

我正在尝试运行基本a代码:

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\u networkx(H)
,默认为
,标签为True

:

图纸(G,位置=无,轴=无,**kwds)

[……]

kwds(可选关键字)–有关可选关键字的说明,请参见networkx.draw\u networkx()

绘制网络x(G,位置=无,箭头=真,带标签=真,**kwds)

[……]

带_标签(bool,可选(默认值=True))–设置为True可在节点上绘制标签

编辑:

使用不同颜色绘制边:

查看函数

相关属性:

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')

使用nx.draw\u networkx代替nx.draw它正在工作,感谢没有问题。顺便说一句,您可以使用
nx生成相同的图形。完整的图形(7)
@warped您知道如何更改边的颜色吗?我并不是想画一个完整的图,我只是想画一个完整的图,但是有不同颜色的边。请看我的文章