主要问题正如 EdChum 所回答的那样。
但是,您似乎正在使用 ipython。在这种情况下,即使包含 with_labels=True 也可能会失败。如果您绘制并保存它,它将具有标签,但不会使用plt.show() 命令显示标签。这是一个错误,(根据 cmets)已在即将发布的版本中修复。
如果我将您的代码编辑为 with_labels=True:
import networkx as nx
import matplotlib.pyplot as plt
def simple_graph():
#create an empty graph
G = nx.Graph()
#add three edges
G.add_edge('A','B');
G.add_edge('B','C');
G.add_edge('C','A');
#draw the graph
nx.draw(G, with_labels=True)
#show
plt.show()
simple_graph()
并在 ipython 中运行它,我得到了绘图,但没有出现标签,并且错误消息:
>Traceback (most recent call last):
> File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", >line 59, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/figure.py", line 1079, in draw
func(*args)
File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 2092, in draw
a.draw(renderer)
File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/text.py", line 538, in draw
bbox, info, descent = self._get_layout(renderer)
File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/text.py", line 311, in _get_layout
ismath=False)
File "/Users/xxx/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 166, in get_text_width_height_descent
six.text_type(s), family, size, weight, style)
TypeError: bad argument type for built-in operation
这是一个已知的错误。一些细节可以在这里找到:pylab/networkx; no node labels displayed after update