Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.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 基于列表/目录动态更改networkx中箭头的大小_Python_Data Visualization_Networkx - Fatal编程技术网

Python 基于列表/目录动态更改networkx中箭头的大小

Python 基于列表/目录动态更改networkx中箭头的大小,python,data-visualization,networkx,Python,Data Visualization,Networkx,我可以通过向draw_网络函数传递一个值列表来动态更改节点大小或节点颜色。但是我怎样才能用ArrowStyle呢?假设我想根据值列表更改箭头样式(宽度和长度)。arrowsize不接受除单个int值以外的任何内容 下面是一个示例代码: import matplotlib.patches import networkx as nx G = nx.DiGraph() G.add_edge("reddit", "youtube") G.add_edge("reddit", "google") G.ad

我可以通过向draw_网络函数传递一个值列表来动态更改节点大小或节点颜色。但是我怎样才能用ArrowStyle呢?假设我想根据值列表更改箭头样式(宽度和长度)。arrowsize不接受除单个int值以外的任何内容

下面是一个示例代码:

import matplotlib.patches
import networkx as nx
G = nx.DiGraph()
G.add_edge("reddit", "youtube")
G.add_edge("reddit", "google")
G.add_edge("google", "reddit")
G.add_edge("youtube", "reddit")
print(G.adj)

testArrow = matplotlib.patches.ArrowStyle.Fancy(head_length=.4, head_width=.4, tail_width=.1)


nx.draw_networkx(G,
             arrowsize=30,
             node_size=[5000,50,300],
             arrowstyle=testArrow)

更改箭头的尺寸:

根据《金融时报》的报道,在一次通话中没有这样做的选择。但是,可以按如下方式逐个绘制边缘:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph()
G.add_edge("reddit", "youtube", w=5)
G.add_edge("reddit", "google", w=10)
G.add_edge("google", "reddit", w=30)
G.add_edge("youtube", "reddit", w=20)
print(G.adj)

pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, node_size=[5000, 50, 300])
nx.draw_networkx_labels(G, pos)
for edge in G.edges(data=True):
    w = edge[2]['w']
    nx.draw_networkx_edges(G, pos, edgelist=[(edge[0],edge[1])], arrowsize=w, node_size=[5000, 50, 300])
plt.show()
这将带来如下结果:


用于更改边的尺寸:

边的宽度和长度在概念上存在差异。虽然宽度是可配置的,并且可以很容易地设置每条边,但长度是由节点的位置定义的

要以类似于节点大小和颜色的方式更改边的宽度,可以调用,参数“width”接受浮点或浮点数组


要更改边的长度,必须更改由nx.draw\u networkx中的“pos”参数设置的。默认设置是使用spring布局定位。

您好,我的问题可能不清楚。我只对更改箭头样式感兴趣。我想要一个大箭头表示具有较大权重的定向边,一个小箭头表示具有较低权重的定向边。我在我的问题中添加了另一幅图片。事实上,我一开始就错了,编辑了答案。我希望你会发现它有用:)谢谢,这就是我要找的!