networkx 画有向图 使用DiGraph

 

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.getnode, 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_layoutG)
nx.draw_networkx_nodesG, pos, cmap=plt.get_cmap'jet'), 
                       node_color = values, node_size = 500)
nx.draw_networkx_labelsG, pos)
nx.draw_networkx_edgesG, pos, edgelist=red_edges, edge_color='r', arrows=True)
nx.draw_networkx_edgesG, pos, edgelist=black_edges, arrows=False)
plt.show)


边上标记weight或者label:
import networkx as nx

# Sample graph
G = nx.Graph)
G.add_edge0, 1)
G.add_edge1, 2)
G.add_edge2, 3)
G.add_edge1, 3)

labels = {0, 1): 'foo', 2, 3): 'bar'}

pos = nx.spring_layoutG)

nx.drawG, pos)
nx.draw_networkx_edge_labelsG, pos, edge_labels=labels, font_size=30)

import pylab as plt
plt.show)


----

I like to do it like this:

import matplotlib.pyplot as plt
pos=nx.spring_layoutG) # pos = nx.nx_agraph.graphviz_layoutG)
nx.draw_networkxG,pos)
labels = nx.get_edge_attributesG,'weight')
nx.draw_networkx_edge_labelsG,pos,edge_labels=labels)
 

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注