pylab/networkx; no node labels displayed after update

Posted on

Question :

pylab/networkx; no node labels displayed after update

After I have updated matplotlib to the current version I encounter a problem with node labels in networkX: if I use the nx.draw(G) command, I get a graph, but no labels are displayed. But let’s speak with examples:

import networkx as nx
import matplotlib.pylab as plt

T=nx.Graph()
T.add_edge(0,1)
nx.draw(T)
plt.show()

this returns a valid plot, but with no node labels. Even if I pass the labels directly like in

import networkx as nx
import matplotlib.pylab as plt

T=nx.Graph()
T.add_edge(0,1)
labs={}
labs[0]='cake'
labs[1]='cookie'
nx.draw(T,labels=labs)
plt.show()

there are still no labels. I’m pretty sure it (especially the upper one) was working yesterday before the update. So was there a change from matplotlib 1.3.x (don’t remember the exact one I was running previously)? The current versions are:

matplotlib (1.4.0)   (had to downgrade the pyparsing to 1.5.7 after updating the matplotlib)
networkx (1.9)
python 2.7.6
Mac OS X 10.9.4

Little Extra: if i run the upper code with nx.draw(T, with_labels=True) I get a plot (extra window on my settings) and when I close it a TypeError: bad argument type for built-in operation pops up. It does not happen if I run nx.draw(T, with_labels=False), which is very confusing, since I thought with_labels argument takes a boolean (see here), which it (partially) does not….?

Am I misunderstanding something here?

Edit: @tcaswell hope that helps!

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     57     def draw_wrapper(artist, renderer, *args, **kwargs):
     58         before(artist, renderer)
---> 59         draw(artist, renderer, *args, **kwargs)
     60         after(artist, renderer)
     61 

/usr/local/lib/python2.7/site-packages/matplotlib/figure.pyc in draw(self, renderer)
   1077         dsu.sort(key=itemgetter(0))
   1078         for zorder, a, func, args in dsu:
-> 1079             func(*args)
   1080 
   1081         renderer.close_group('figure')

/usr/local/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     57     def draw_wrapper(artist, renderer, *args, **kwargs):
     58         before(artist, renderer)
---> 59         draw(artist, renderer, *args, **kwargs)
     60         after(artist, renderer)
     61 

/usr/local/lib/python2.7/site-packages/matplotlib/axes/_base.pyc in draw(self, renderer, inframe)
   2090 
   2091         for zorder, a in dsu:
-> 2092             a.draw(renderer)
   2093 
   2094         renderer.close_group('axes')

/usr/local/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     57     def draw_wrapper(artist, renderer, *args, **kwargs):
     58         before(artist, renderer)
---> 59         draw(artist, renderer, *args, **kwargs)
     60         after(artist, renderer)
     61 

/usr/local/lib/python2.7/site-packages/matplotlib/text.pyc in draw(self, renderer)
    536         renderer.open_group('text', self.get_gid())
    537 
--> 538         bbox, info, descent = self._get_layout(renderer)
    539         trans = self.get_transform()
    540 

/usr/local/lib/python2.7/site-packages/matplotlib/text.pyc in _get_layout(self, renderer)
    309         tmp, lp_h, lp_bl = renderer.get_text_width_height_descent('lp',
    310                                                          self._fontproperties,
--> 311                                                          ismath=False)
    312         offsety = (lp_h - lp_bl) * self._linespacing
    313 

/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.pyc in get_text_width_height_descent(self, s, prop, ismath)
    164         size = self.points_to_pixels(points)
    165         width, height, descent = self.gc.get_text_width_height_descent(
--> 166             six.text_type(s), family, size, weight, style)
    167         return  width, height, 0.0*descent
    168 

TypeError: bad argument type for built-in operation

(I’m neither a mathematician nor a programmer, but willing to learn, so please be patient!)

Asked By: olga.bio

||

Answer #1:

This is hitting a known (but unresolved) bug in the OSX backend (#3470).

Switching to one of the Agg based backends should ‘resolve’ the problem by avoiding it.

UPDATE

Part of the problem has been identified and merged https://github.com/matplotlib/matplotlib/pull/3564. This fix should be in mpl v1.4.1 and higher.

Answered By: olga.bio

Leave a Reply

Your email address will not be published.