파이썬에서 networkx를 사용하여 방향성 그래프를 그리는 방법?
그래프에 매핑하려는 스크립트에서 나오는 노드가 있습니다. 아래에서 Arrow를 사용하여 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', '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()
하지만 이미지에 표시된 것과 같은 것을 원합니다.

첫 번째 이미지의 화살표 머리와 두 번째 이미지의 붉은 색 가장자리 .. 감사합니다
빨간색 가장자리에만 화살표가있는 완전히 구체화 된 예제 :
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()
I only put this in for completeness. I've learned plenty from marius and mdml. Here are the edge weights. Sorry about the arrows. Looks like I'm not the only one saying it can't be helped. I couldn't render this with ipython notebook I had to go straight from python which was the problem with getting my edge weights in sooner.
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import pylab
G = nx.DiGraph()
G.add_edges_from([('A', 'B'),('C','D'),('G','D')], weight=1)
G.add_edges_from([('D','A'),('D','E'),('B','D'),('D','E')], weight=2)
G.add_edges_from([('B','C'),('E','F')], weight=3)
G.add_edges_from([('C','F')], weight=4)
val_map = {'A': 1.0,
'D': 0.5714285714285714,
'H': 0.0}
values = [val_map.get(node, 0.45) for node in G.nodes()]
edge_labels=dict([((u,v,),d['weight'])
for u,v,d in G.edges(data=True)])
red_edges = [('C','D'),('D','A')]
edge_colors = ['black' if not edge in red_edges else 'red' for edge in G.edges()]
pos=nx.spring_layout(G)
nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels)
nx.draw(G,pos, node_color = values, node_size=1500,edge_color=edge_colors,edge_cmap=plt.cm.Reds)
pylab.show()

You need to use a directed graph instead of a graph, i.e.
G = nx.DiGraph()
Then, create a list of the edge colors you want to use and pass those to nx.draw (as shown by @Marius).
Putting this all together, I get the image below. Still not quite the other picture you show (I don't know where your edge weights are coming from), but much closer! If you want more control of how your output graph looks (e.g. get arrowheads that look like arrows), I'd check out NetworkX with Graphviz.

Instead of regular nx.draw you may want to use:
nx.draw_networkx(G[, pos, arrows, with_labels])
For example:
nx.draw_networkx(G, arrows=True, **options)
You can add options by initialising that ** variable like this:
options = {
'node_color': 'blue',
'node_size': 100,
'width': 3,
'arrowstyle': '-|>',
'arrowsize': 12,
}
Also some functions support the directed=True parameter In this case this state is the default one:
G = nx.DiGraph(directed=True)
The networkx reference is found here.

import networkx as nx
import matplotlib.pyplot as plt
g = nx.DiGraph()
g.add_nodes_from([1,2,3,4,5])
g.add_edge(1,2)
g.add_edge(4,2)
g.add_edge(3,5)
g.add_edge(2,3)
g.add_edge(5,4)
nx.draw(g,with_labels=True)
plt.draw()
plt.show()
This is just simple how to draw directed graph using python 3.x using networkx. just simple representation and can be modified and colored etc. See the generated graph here.
참고 : 이것은 단순한 표현 일뿐입니다. Weighted Edges는 다음과 같이 추가 할 수 있습니다.
g.add_edges_from([(1,2),(2,5)], weight=2)
따라서 다시 플롯됩니다.
참고 URL : https://stackoverflow.com/questions/20133479/how-to-draw-directed-graphs-using-networkx-in-python
'Programing' 카테고리의 다른 글
| 잠그지 않고 텍스트 파일을 어떻게 읽을 수 있습니까? (0) | 2020.09.19 |
|---|---|
| 외래 키를 기본 키로 사용해도 괜찮습니까? (0) | 2020.09.19 |
| go get으로 "인식 할 수없는 가져 오기 경로" (0) | 2020.09.19 |
| Visual Basic .NET에서 Char 리터럴을 어떻게 선언합니까? (0) | 2020.09.19 |
| auto_ptr이 더 이상 사용되지 않는 이유는 무엇입니까? (0) | 2020.09.19 |
