Python networkx - 如何用标签绘制图形

Python networkx - How to draw graph with labels

from graphviz import *
import networkx as nx
from networkx import *
import matplotlib.pyplot as plt

    G = nx.DiGraph()
    G.add_node(1)
    G.add_node(2)
    G.add_edge(1,2)
    myLabels = {1: 'node1', 2: 'node2'}
    nx.set_node_attributes(G, myLabels, 'label')
    nx.draw(G,with_labels=True)

所以目前我用的是最新的networkx。当我使用 nx.draw(G, with_values=True) 时,它使用顶点索引而不是标签。

我该如何解决这个问题?谢谢。

更改此命令:nx.draw(G,with_labels=True)nx.draw(G,with_labels=True, labels = myLabels)