The main problem is as EdChum answered.
However, it looks like you may be using using ipython. In that case, even including with_labels=True
can fail. If you plot it and save it it will have the label, but it won't display the label with the plt.show()
command. This is a bug which (according to comments) has been fixed in an upcoming release.
If I edit your code to have 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()
and run it in ipython I get the plot, but without labels appearing, and the error message:
>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
This is a known bug. Some details can be found here: pylab/networkx; no node labels displayed after update