使用Networkx绘制带边缘的图形

使用Networkx绘制带边缘的图形

本文介绍了使用Networkx绘制带边缘的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直很困扰一些非常简单的事情。我试图绘制并显示2个节点和1个边的图。但是我得到这个错误:

pre $ Traceback(最近一次调用最后一次):
文件< stdin>,第1行,在< module>
文件/usr/local/lib/python2.7/site-packages/networkx/drawing/nx_pylab.py,第133行,在draw
draw_networkx(G,pos = pos,ax = ax ,** kwds)
文件/usr/local/lib/python2.7/site-packages/networkx/drawing/nx_pylab.py,第267行,在draw_networkx中
edge_collection = draw_networkx_edges(G, pos,** kwds)
文件/usr/local/lib/python2.7/site-packages/networkx/drawing/nx_pylab.py,第546行,在draw_networkx_edges
ax.add_collection(edge_collection )
文件/usr/local/lib/python2.7/site-packages/matplotlib/axes.py,第1445行,在add_collection
self.update_datalim(collection.get_datalim(self.transData) )
文件/usr/local/lib/python2.7/site-packages/matplotlib/collections.py,第167行,在get_datalim
offsetsets.shape =(-1,2)#Make它Nx2
AttributeError:非连续数组的不兼容形状

它非常简单,我只需添加2个节点:$ b​​
$ b

  add_node(1)
add_node(2)
add_edge(1,2)

然后绘制并显示它。但是我只是无法让它工作。

解决方案

这是一个解决方案。

 导入matplotlib.pyplot为plt 
导入networkx为nx

#创建基本图形
g = nx.Graph ()
g.add_node(1)
g.add_node(2)
g.add_edge(1,2)

#输出结果
nx.draw(g)
plt.show()


I've been troubled by something very simple. I am trying to draw and show a graph with 2 nodes and 1 edge. But I get this error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/networkx/drawing/nx_pylab.py", line 133, in      draw
draw_networkx(G,pos=pos,ax=ax,**kwds)
File "/usr/local/lib/python2.7/site-packages/networkx/drawing/nx_pylab.py", line 267, in draw_networkx
edge_collection=draw_networkx_edges(G, pos, **kwds)
File "/usr/local/lib/python2.7/site-packages/networkx/drawing/nx_pylab.py", line 546, in draw_networkx_edges
ax.add_collection(edge_collection)
File "/usr/local/lib/python2.7/site-packages/matplotlib/axes.py", line 1445, in add_collection
self.update_datalim(collection.get_datalim(self.transData))
File "/usr/local/lib/python2.7/site-packages/matplotlib/collections.py", line 167, in get_datalim
offsets.shape = (-1, 2)                     # Make it Nx2
AttributeError: incompatible shape for a non-contiguous array

It is very simple, I just add 2 nodes with

add_node(1)
add_node(2)
add_edge(1,2)

and then draw it and show it. But I just cant get it to work.

解决方案

Here is a solution that works.

import matplotlib.pyplot as plt
import networkx as nx

# Create a basic Graph
g=nx.Graph()
g.add_node(1)
g.add_node(2)
g.add_edge(1,2)

# Output the result
nx.draw(g)
plt.show()

这篇关于使用Networkx绘制带边缘的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:39