numpy - 使用 Python/iPython 的网络绘图错误

标签 numpy matplotlib ipython networkx

当我尝试在 OSX 10.8.2 上的 IPython 笔记本中使用 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

最佳答案

这已在 Draw a graph with edges using Networkx 中早些时候报道过。如上所述,这可能是 NumPy 库开发版本中的错误/功能(numpy 问题 2700)。

关于numpy - 使用 Python/iPython 的网络绘图错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13333738/

相关文章:

python - 如何更改 julia PyPlot 极坐标图中的径向刻度?

python - 如何在ipython中获得不同的点

python - IPython 笔记本将代码单元放入列中

python - 绕过 "Array is too big"python错误

python - numpy 中的一些奇怪的东西

python - 带有空标记的 Matplotlib 绘图线

Python matplotlib 等高线图对数色标

ipython - 导入错误 : dynamic module does not define module export function (PyInit_cv2)

python - 索引错误 : list index out of range - cannot reason the logic

python - 使用空字符串或 A 列中的值(具体取决于 B 列上的值)在 pandas 数据框中添加新列