如何在Python中使用networkx绘制有向图? [英] how to draw directed graphs using networkx in python?

查看:309
本文介绍了如何在Python中使用networkx绘制有向图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些节点来自我想要映射到图形的脚本。在下面,我想使用箭头从A到D,并且可能在(红色或某物)中也有着色边缘。
这基本上就是当所有其他节点都存在时从A到D的路径。您可以将每个节点想象为城市,从A到D旅行需要方向(带箭头)。
下面的代码构建图表

  import networkx as nx 
import numpy as np
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_edges_from(
[('A','B'),('A',' ('E','C'),('E','F'),
('B','H'),(' B','G'),('B','F'),('C','G')])

val_map = {'A':1.0,
'D':
'H':0.0}

values = [val_map.get(node,0.25)for node in G.nodes()]

nx.draw(G,cmap = plt.get_cmap('jet'),node_color = values)
plt.show()

但我想要的东西像图中所示

第一个图像的箭头和红色的边缘到t上他的第二张图片。谢谢

解决方案

完全充实的例子只有红色边缘的箭头:

  import networkx as nx 
import matplotlib.pyplot as plt

G = nx.DiGraph()
G.add_edges_from('A','B'),('A','C'),('D','B'),('E','C'),( '','F'),
('B','H'),('B','G'),('B','F'),('C','G ')])

val_map = {'A':1.0,
'D':0.5714285714285714,
'H':0.0}

值= [val_map.get(node,0.25)for node in G.nodes()]

#指定你想要的边在这里
red_edges = [('A','C') ,('E','C')]
edge_colours = ['black'if red_edges else if'red'
for gedged()]
black_edges = [如果edge不在red_edges中,则在G.edges()中为边设置边缘]

#在执行
时需要创建布局#分开调用来绘制节点和边
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G,pos,cmap = plt.get_cmap('jet'),
node_color = values ,node_size = 500)
nx.draw_networkx_labels(G,pos)
nx.draw_networkx_edges(G,pos,edgelist = red_edges,edge_color ='r',arrows = True)
nx.draw_networkx_edges (G,pos,edgelist = black_edges,arrows = False)
plt.show()


I have some nodes coming from a script that I want to map on to a graph. In the below, I want to use Arrow to go from A to D and probably have the edge colored too in (red or something). This is basically, like a path from A to D when all other nodes are present. you can imagine each nodes as cities and travelling from A to D requires directions (with arrow heads). This code below builds the graph

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_edges_from(
    [('A', 'B'), ('A', 'C'), ('D', 'B'), ('E', 'C'), ('E', 'F'),
     ('B', 'H'), ('B', 'G'), ('B', 'F'), ('C', 'G')])

val_map = {'A': 1.0,
           'D': 0.5714285714285714,
           'H': 0.0}

values = [val_map.get(node, 0.25) for node in G.nodes()]

nx.draw(G, cmap = plt.get_cmap('jet'), node_color = values)
plt.show()

but I want something like shown in the image.

Arrow heads of the first image and the edges in red color onto the second image..Thanks

解决方案

Fully fleshed out example with arrows for only the red edges:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph()
G.add_edges_from(
    [('A', 'B'), ('A', 'C'), ('D', 'B'), ('E', 'C'), ('E', 'F'),
     ('B', 'H'), ('B', 'G'), ('B', 'F'), ('C', 'G')])

val_map = {'A': 1.0,
           'D': 0.5714285714285714,
           'H': 0.0}

values = [val_map.get(node, 0.25) for node in G.nodes()]

# Specify the edges you want here
red_edges = [('A', 'C'), ('E', 'C')]
edge_colours = ['black' if not edge in red_edges else 'red'
                for edge in G.edges()]
black_edges = [edge for edge in G.edges() if edge not in red_edges]

# Need to create a layout when doing
# separate calls to draw nodes and edges
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, cmap=plt.get_cmap('jet'), 
                       node_color = values, node_size = 500)
nx.draw_networkx_labels(G, pos)
nx.draw_networkx_edges(G, pos, edgelist=red_edges, edge_color='r', arrows=True)
nx.draw_networkx_edges(G, pos, edgelist=black_edges, arrows=False)
plt.show()

这篇关于如何在Python中使用networkx绘制有向图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆