简体   繁体   中英

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)

So current I use the latest.networkx. When I use nx.draw(G, with_values=True), it uses the vertex indexes instead of labels.

How can I fix this? Thank you.

图片应该是 node1, node2

Change this command: nx.draw(G,with_labels=True) to nx.draw(G,with_labels=True, labels = myLabels)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM