如何使用networkx在图形中显示节点名称?

时间:2015-10-01 18:14:46

标签: python matplotlib graph networkx

我有代码

import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph()

G.add_nodes_from([1, 2, 3, 4])
G.add_edges_from([(1, 2), (2, 1), (2, 3)])

nx.draw(G)
plt.savefig("graph.png")
plt.show()

它绘制了以下图表: enter image description here

但是,我需要显示标签。 如何在图形的节点中显示数值和单词(一,二,三和四)?

1 个答案:

答案 0 :(得分:18)

您只需要使用nx.Draw()调用 with_labels = True 参数:

>>> pandas.DataFrame(active_posts)
   la  life  ny  uk
0  18     9  84   4
1   6    18  27   3

您也可以调用font_size,font_color等

https://networkx.github.io/documentation/latest/reference/drawing.html