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.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()


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

# Sample graph
G = nx.Graph()
G.add_edge(0, 1)
G.add_edge(1, 2)
G.add_edge(2, 3)
G.add_edge(1, 3)

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

pos = nx.spring_layout(G)

nx.draw(G, pos)
nx.draw_networkx_edge_labels(G, 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_layout(G) # pos = nx.nx_agraph.graphviz_layout(G) nx.draw_networkx(G,pos) labels = nx.get_edge_attributes(G,'weight') nx.draw_networkx_edge_labels(G,pos,edge_labels=labels)
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM