我有密码
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()
并画出下图:
但是,我需要显示标签。
如何显示图形节点内的数值和单词(1、2、3和4)?
最佳答案
只需使用nx.draw()调用with_labels=true参数:
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,with_labels=True)
plt.savefig("graph.png")
plt.show()
您还可以调用字体大小、字体颜色等。
https://networkx.github.io/documentation/latest/reference/drawing.html
关于python - 如何使用networkx在图形中显示节点名称?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32894223/