我正在try 编写Python代码,希望能够更改每条边的宽度.

这是我到目前为止的代码:

import matplotlib.pyplot as plt
import networkx as nx

# create Graph

g = nx.Graph()

# add nodes to 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_node(8)
g.add_node(9)
g.add_node(10)
g.add_node(11)
g.add_node(12)
g.add_node(13)
g.add_node(14)
g.add_node(15)
g.add_node(16)
g.add_node(17)
g.add_node(18)
g.add_node(19)
g.add_node(20)
g.add_node(21)

# add edges between nodes

g.add_edge(1, 2,width=1)
g.add_edge(1, 3,width=10)
g.add_edge(2, 3,width=10)
g.add_edge(3, 4,width=10)
g.add_edge(4, 5,width=1)
g.add_edge(5, 6,width=10)
g.add_edge(5, 7,width=10)
g.add_edge(6, 7,width=10)
g.add_edge(5, 9,width=10)
g.add_edge(6, 9,width=10)
g.add_edge(7, 8,width=10)
g.add_edge(6, 10,width=10)
g.add_edge(10, 11,width=10)
g.add_edge(6, 11,width=10)
g.add_edge(11, 12,width=10)
g.add_edge(9, 10,width=10)
g.add_edge(9, 15,width=10)
g.add_edge(10, 13,width=10)
g.add_edge(12, 13,width=10)
g.add_edge(13, 14,width=10)
g.add_edge(14, 16,width=10)
g.add_edge(15, 14,width=10)
g.add_edge(9, 14,width=10)
g.add_edge(16, 17,width=10)
g.add_edge(13, 18,width=10)
g.add_edge(17, 18,width=10)
g.add_edge(17, 20,width=10)
g.add_edge(17, 19,widht=10)
g.add_edge(19, 20,width=10)
g.add_edge(18, 20,width=10)
g.add_edge(19, 21,width=10)
g.add_edge(20, 21,width=10)

widths = list(nx.get_edge_attributes(g,'width').values())



# position of nodes of Graph
pos={
    1:(13,78),
    2:(24,87),
    3:(23,77),
    4:(25,68),
    5:(27,59),
    6:(27,46),
    7:(17,49),
    8:(5,36),
    9:(42,58),
    10:(40,36),
    11:(35,31),
    12:(40,12),
    13:(61,30),
    14:(70,43),
    15:(72,55),
    16:(82,40),
    17:(83,20),
    18:(77,15),
    19:(93,14),
    20:(88,9),
    21:(97,4),
}

# drawing graph and specifying properties(node color, edge color, line width, etc)
nx.draw(g, pos, with_labels=True,node_color="orange", node_size=300, edge_color="black", width=list(widths))


# The graph title and showing it
plt.title('Gabriel Graph (102)')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

问题是,我有所有的边缘宽度设置为10,除了2,但输出显示三个边缘的宽度为1.

以下是指向输出的链接:https://i.stack.imgur.com/akgwo.png

有谁能帮我修一下这个吗?

推荐答案

欢迎来到So.这是一个很好的极小的、可重现的例子.

正如我在上面的 comments 中指出的,您有一个打字错误.因此,作为width参数提供给draw的列表少了一个.在这种情况下,networkx应该会引发一个错误,但它没有.相反,它使用默认宽度1,这就是为什么为您的"最后"边(20,21)分配的宽度比指定的要小.

如果避免代码重复,这样的打字错误是可以避免的.我会这样构建图表:

import networkx as nx

# create Graph
g = nx.Graph()
g.add_nodes_from(range(1, 22))
g.add_edges_from([(1, 2), (4, 5)], width=1)
g.add_edges_from([
    (1  ,  3),
    (2  ,  3),
    (3  ,  4),
    (5  ,  6),
    (5  ,  7),
    (5  ,  9),
    (6  ,  7),
    (6  ,  9),
    (7  ,  8),
    (6  , 10),
    (6  , 11),
    (9  , 10),
    (9  , 14),
    (9  , 15),
    (10 , 11),
    (10 , 13),
    (11 , 12),
    (12 , 13),
    (13 , 14),
    (13 , 18),
    (14 , 16),
    (15 , 14),
    (16 , 17),
    (17 , 18),
    (17 , 20),
    (17 , 19),
    (18 , 20),
    (19 , 20),
    (19 , 21),
    (20 , 21),
], width=10)

Python相关问答推荐

PyTorch卷积自动编码器,输出维度与输入不同

如何使用Python中的clinicalTrials.gov API获取完整结果?

拆分pandas列并创建包含这些拆分值计数的新列

Python json.转储包含一些UTF-8字符的二元组,要么失败,要么转换它们.我希望编码字符按原样保留

需要计算60,000个坐标之间的距离

如何标记Spacy中不包含特定符号的单词?

优化pytorch函数以消除for循环

将pandas Dataframe转换为3D numpy矩阵

优化器的运行顺序影响PyTorch中的预测

使用密钥字典重新配置嵌套字典密钥名

计算分布的标准差

如何启动下载并在不击中磁盘的情况下呈现响应?

在Python中调用变量(特别是Tkinter)

dask无groupby(ddf. agg([min,max])?''''

幂集,其中每个元素可以是正或负""""

如何防止Pandas将索引标为周期?

python—telegraph—bot send_voice发送空文件

巨 Python :逆向猜谜游戏

在Docker容器(Alpine)上运行的Python应用程序中读取. accdb数据库

mdates定位器在图表中显示不存在的时间间隔