0

I have installed networkx and matplotlib using pip on Mac Os 10.10.3 for python 3.3.

When I run the following code:

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)

    #show
    plt.show()

simple_graph()

I get the graph as expected but all the text is missing. Any suggestions why?

Baz
  • 12,713
  • 38
  • 145
  • 268

2 Answers2

1

Pass param 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()

Yields the plot:

enter image description here

If instead you call draw_networkx:

enter image description here

Essentially nx.draw calls nx.draw_networkx but without certain params set.

EdChum
  • 376,765
  • 198
  • 813
  • 562
0

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

Community
  • 1
  • 1
Joel
  • 22,598
  • 6
  • 69
  • 93