<p>一个带有<code>networkx</code>包的示例。我们需要您提供的<code>Weights</code>来构建图。在</p>
<pre><code>import matplotlib.pyplot as plt
import networkx as nx
%matplotlib notebook
Weights = {('M', 'C'): 44, ('Q', 'F'): 27, ('Y', 'X'): 42, ('X', 'Y'): 42, ('Y', 'M'): 6, ('M', 'F'): 9, ('M', 'Y'): 6, ('F', 'Q'): 27, ('F', 'M'): 9, ('C', 'M'): 44}
G = nx.Graph()
# each edge is a tuple of the form (node1, node2, {'weight': weight})
edges = [(k[0], k[1], {'weight': v}) for k, v in Weights.items()]
G.add_edges_from(edges)
pos = nx.spring_layout(G) # positions for all nodes
# nodes
nx.draw_networkx_nodes(G,pos,node_size=700)
# labels
nx.draw_networkx_labels(G,pos,font_size=20,font_family='sans-serif')
# edges
nx.draw_networkx_edges(G,pos,edgelist=edges, width=6)
# weights
labels = nx.get_edge_attributes(G,'weight')
nx.draw_networkx_edge_labels(G,pos,edge_labels=labels)
</code></pre>
<p>布局</p>
<p><a href="https://i.stack.imgur.com/TnK4P.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TnK4P.png" alt="enter image description here"/></a></p>
<p>代码从此<a href="https://networkx.github.io/documentation/networkx-1.9/examples/drawing/weighted_graph.html" rel="nofollow noreferrer">Tutorial by Aric Hagberg</a>和<a href="https://stackoverflow.com/questions/28372127/add-edge-weights-to-plot-output-in-networkx">answer by Marcus Müller</a>修改。在</p>