pylab / 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
小额外:如果我用nx.draw(T, with_labels=True)
运行上面的代码,我会得到一个图(我的设置上有额外的窗口),当我关闭它时TypeError: bad argument type for built-in operation
弹出一个TypeError: bad argument type for built-in operation
。 如果我运行nx.draw(T, with_labels=False)
,这不会发生,因为我认为with_labels
参数需要一个布尔值(请参见此处),它(部分)不会......。
我在这里误解了什么吗?
编辑:@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
(我既不是数学家也不是程序员,但愿意学习,所以请耐心等待!)
这是OSX后端中已知的(但未解决的)错误(#3470)。
切换到其中一个基于Agg的后端应该通过避免它来“解决”问题。
UPDATE
部分问题已被识别并合并https://github.com/matplotlib/matplotlib/pull/3564。 这个修复应该在mpl v1.4.1及更高版本中。
链接地址: http://www.djcxy.com/p/81479.html