使用Python / iPython的网络图错误

时间:2012-11-11 17:49:16

标签: numpy matplotlib ipython networkx

当我在IPython笔记本中尝试在OSX 10.8.2上使用networkx时,以下启动码错误(AttributeError:非连续数组的不兼容形状)。

import networkx as nx
g = nx.Graph()
g.add_node(1)
g.add_node(2)
g.add_edge(1,2) # no error if this line is omitted
nx.draw(g)

如果我没有为此图添加边缘,则看不到错误。 Matplotlib,Numpy和Scipy都按照建议here安装。

以下是错误消息:


AttributeError                            Traceback (most recent call last)

<ipython-input-33-df7dfeff6452> in <module>()
----> 1 nx.draw(g)

/usr/local/lib/python2.7/site-packages/networkx/drawing/nx_pylab.pyc in draw(G, pos, ax, hold, **kwds)
    131         pylab.hold(h)
    132     try:
--> 133         draw_networkx(G,pos=pos,ax=ax,**kwds)
    134         ax.set_axis_off()
    135         pylab.draw_if_interactive()

/usr/local/lib/python2.7/site-packages/networkx/drawing/nx_pylab.pyc in draw_networkx(G, pos, with_labels, **kwds)
    265 
    266     node_collection=draw_networkx_nodes(G, pos, **kwds)
--> 267     edge_collection=draw_networkx_edges(G, pos, **kwds)
    268     if with_labels:
    269         draw_networkx_labels(G, pos, **kwds)

/usr/local/lib/python2.7/site-packages/networkx/drawing/nx_pylab.pyc in draw_networkx_edges(G, pos, edgelist, width, edge_color, style, alpha, edge_cmap, edge_vmin, edge_vmax, ax, arrows, label, **kwds)
    544     edge_collection.set_zorder(1) # edges go behind nodes
    545     edge_collection.set_label(label)
--> 546     ax.add_collection(edge_collection)
    547 
    548     # Note: there was a bug in mpl regarding the handling of alpha values for

/usr/local/lib/python2.7/site-packages/matplotlib/axes.pyc in add_collection(self, collection, autolim)
   1443         if autolim:
   1444             if collection._paths and len(collection._paths):
-> 1445                 self.update_datalim(collection.get_datalim(self.transData))
   1446 
   1447         collection._remove_method = lambda h: self.collections.remove(h)

/usr/local/lib/python2.7/site-packages/matplotlib/collections.pyc in get_datalim(self, transData)
    165             offsets = offsets.filled(np.nan)
    166             # get_path_collection_extents handles nan but not masked arrays
--> 167         offsets.shape = (-1, 2)                     # Make it Nx2
    168 
    169         result = mpath.get_path_collection_extents(

AttributeError: incompatible shape for a non-contiguous array

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/usr/local/lib/python2.7/site-packages/IPython/zmq/pylab/backend_inline.pyc in show(close)
    100     try:
    101         for figure_manager in Gcf.get_all_fig_managers():
--> 102             send_figure(figure_manager.canvas.figure)
    103     finally:
    104         show._to_draw = []

/usr/local/lib/python2.7/site-packages/IPython/zmq/pylab/backend_inline.pyc in send_figure(fig)
    209     """
    210     fmt = InlineBackend.instance().figure_format
--> 211     data = print_figure(fig, fmt)
    212     # print_figure will return None if there's nothing to draw:
    213     if data is None:

/usr/local/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in print_figure(fig, fmt)
    102     try:
    103         bytes_io = BytesIO()
--> 104         fig.canvas.print_figure(bytes_io, format=fmt, bbox_inches='tight')
    105         data = bytes_io.getvalue()
    106     finally:

/usr/local/lib/python2.7/site-packages/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   1981                     orientation=orientation,
   1982                     dryrun=True,
-> 1983                     **kwargs)
   1984                 renderer = self.figure._cachedRenderer
   1985                 bbox_inches = self.figure.get_tightbbox(renderer)

/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in print_png(self, filename_or_obj, *args, **kwargs)
    467 
    468     def print_png(self, filename_or_obj, *args, **kwargs):
--> 469         FigureCanvasAgg.draw(self)
    470         renderer = self.get_renderer()
    471         original_dpi = renderer.dpi

/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in draw(self)
    419 
    420         try:
--> 421             self.figure.draw(self.renderer)
    422         finally:
    423             RendererAgg.lock.release()

/usr/local/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53     def draw_wrapper(artist, renderer, *args, **kwargs):
     54         before(artist, renderer)
---> 55         draw(artist, renderer, *args, **kwargs)
     56         after(artist, renderer)
     57 

/usr/local/lib/python2.7/site-packages/matplotlib/figure.pyc in draw(self, renderer)
    896         dsu.sort(key=itemgetter(0))
    897         for zorder, a, func, args in dsu:
--> 898             func(*args)
    899 
    900         renderer.close_group('figure')

/usr/local/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53     def draw_wrapper(artist, renderer, *args, **kwargs):
     54         before(artist, renderer)
---> 55         draw(artist, renderer, *args, **kwargs)
     56         after(artist, renderer)
     57 

/usr/local/lib/python2.7/site-packages/matplotlib/axes.pyc in draw(self, renderer, inframe)
   1995 
   1996         for zorder, a in dsu:
-> 1997             a.draw(renderer)
   1998 
   1999         renderer.close_group('axes')

/usr/local/lib/python2.7/site-packages/matplotlib/artist.pyc in draw_wrapper(artist, renderer, *args, **kwargs)
     53     def draw_wrapper(artist, renderer, *args, **kwargs):
     54         before(artist, renderer)
---> 55         draw(artist, renderer, *args, **kwargs)
     56         after(artist, renderer)
     57 

/usr/local/lib/python2.7/site-packages/matplotlib/collections.pyc in draw(self, renderer)
    227         self.update_scalarmappable()
    228 
--> 229         transform, transOffset, offsets, paths = self._prepare_points()
    230 
    231         gc = renderer.new_gc()

/usr/local/lib/python2.7/site-packages/matplotlib/collections.pyc in _prepare_points(self)
    203 
    204         offsets = np.asanyarray(offsets, np.float_)
--> 205         offsets.shape = (-1, 2)             # Make it Nx2
    206 
    207         if not transform.is_affine:

AttributeError: incompatible shape for a non-contiguous array

1 个答案:

答案 0 :(得分:2)

早先在Draw a graph with edges using Networkx报道了这一点。它可能是如上所述的NumPy库开发版本中的错误/特性(numpy issue 2700)。