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?

1
  • Are you wanting node labels? you may prefer calling nx.draw_networkx(G) Commented Aug 25, 2015 at 19:26

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

This is fixed on current master and will be released with 1.5
@tcaswell Thanks - please follow up when that is released.
I'll leave that to you. Keep your eye out for a release annoucement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.