将matplotlib更新到当前版本后,在networkX中遇到节点标签问题:如果使用该nx.draw(G)命令,将得到一个图形,但未显示任何标签。但是,让我们用示例进行说明:
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弹出一个窗口。如果我运行nx.draw(T, with_labels=False),它不会发生,这非常令人困惑,因为我认为with_labels参数需要一个布尔值(请参阅此处),(部分地)它不会....?
nx.draw(T, with_labels=True)
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的后端之一应该通过避免它来“解决”问题。
更新
部分问题已被识别并合并https://github.com/matplotlib/matplotlib/pull/3564。此修复程序应该在mpl v1.4.1及更高版本中。