pylab / networkx;更新后没有显示节点标签

时间:2014-09-16 10:08:19

标签: python matplotlib networkx

在我将matplotlib更新到当前版本后,我遇到了networkX中的节点标签问题:如果我使用nx.draw(G)命令,我会得到一个图表,但不显示任何标签。但让我们举个例子说明:

import networkx as nx
import matplotlib.pylab as plt

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

这会返回一个有效的图,但没有节点标签。即使我直接传递标签,如

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

仍然没有标签。我很确定它(特别是上面的)在更新之前昨天正在工作。那么matplotlib 1.3.x是否有变化(不记得我之前运行的那个)?目前的版本是:

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:如果我使用nx.draw(T, with_labels=True)运行上面的代码,我会得到一个情节(我的设置上的额外窗口),当我关闭它时会弹出TypeError: bad argument type for built-in operation。如果我运行nx.draw(T, with_labels=False),这是非常令人困惑的,因为我认为with_labels参数采用布尔值(see here),它(部分)不会....

我在这里误解了什么吗?

编辑:@tcaswell希望有所帮助!

---------------------------------------------------------------------------
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

(我既不是数学家,也不是程序员,但愿意学习,所以请耐心等待!)

1 个答案:

答案 0 :(得分:4)

这是在OSX后端(#3470)中遇到已知(但未解决)的错误。

切换到其中一个基于Agg的后端应该通过避免它来解决问题。

更新

部分问题已被识别并合并https://github.com/matplotlib/matplotlib/pull/3564。此修复应该在mpl v1.4.1及更高版本中。