绘制网络图,所有边缘清晰可见

时间:2015-03-19 02:58:42

标签: python numpy pandas matplotlib

我有30个节点的图形网络数据(使用邻接矩阵)。该图表目前看起来像这样:

enter image description here

每个群集有15个节点,每个节点都连接到同一群集中的其他节点。只有两对不同簇的节点相互连接。问题是我得到的图表都是浓缩的,并且群集中的每个边缘都不清晰可见。有没有办法可以清楚地显示群集中的每个边缘。主要是使图形更大,每个节点的边缘线清晰可见。

我使用networkx lib的以下命令绘制了它。

G1=nx.from_numpy_matrix(W1)
nx.draw_networkx(G1)

其中W1是节点的邻接矩阵(30x30)。

请指教。

编辑:

想要这样的事情,每个节点都清晰,边缘可见而不是浓缩。关键是我希望上部群集点仅显示在该群集附近,而对于较低的群集点则相同。但是在每个集群中,我希望节点有点分开,以便每个边缘都清晰可见。

enter image description here

EDIT2:

def adjacencyMatrix2():
    for x in range(N):
      if (x<15):
          c=N/2
      else: 
          c=N
      for y in range(x+1,c): 
          W1[x][y]=W1[y][x]=1

# Connecting two other nodes separately. 
W1[0][16]=W1[16][0]=1
W1[1][15]=W1[15][1]=1

adjacencyMatrix2()
G1=nx.from_numpy_matrix(W1)
graph_pos=nx.spring_layout(G1,k=0.50,iterations=50)
nx.draw_networkx(G1,graph_pos)

EDIT3:

N=30
# Creating a matrix of zeros. 
W=np.zeros((N,N))
# Mentioning the edges to start with. Thinking of a pair of 15 node cluster with two cluster connected by two pair of nodes. 
edge=[[1,2],[1,3],[1,4],[1,5],[1,6],[1,7],[1,8],[1,9],[1,10],[1,11],[1,12],[1,13],[1,14],[1,15],
      [16,17],[16,18],[16,19],[16,20],[16,21],[16,22],[16,23],[16,24],[16,25],[16,26],[16,27],[16,28],[16,29],[16,30],
      [1,16],[2,17],[2,3],[5,6],[8,9],[9,4],[18,26],[17,22],[29,21],[17,28]]

# Function for creating adjacency matrix ,populating the zeros matrix with 1 and 0-signifying edges on a node. 

def adjacencyMatrix():
    """This function creates an Adjacency Matrix from a edge set. 
    input-> set of edges to be connected 
    output-> Adjacency matrix (n,n)
    """
    for first,second in edge:
        W[first-1,second-1]=W[second-1][first-1]=1

# Creating the adjacency matrix by calling the function.
adjacencyMatrix()

此外,我已经看到每次运行代码时图表的布局都会发生变化。我不希望图形布局随着代码的每次运行而改变。目前它正在这样做。

2 个答案:

答案 0 :(得分:3)

您是否尝试过应用节点定位布局? networkx库支持布局。看看here。我个人会推荐使用Fruchterman-Reingold力导向算法的弹簧布局。该文档为here。要在库中实际运行它,您可以尝试类似:

pos=nx.spring_layout(G1)

其中pos是每个节点键入的位置字典。

编辑:您可以使用上述文档中引用的参数控制布局的确切间距。具体来说,如:

nx.spring_layout(G,k=0.15,iterations=20)
# k controls the distance between the nodes and varies between 0 and 1
# iterations is the number of times simulated annealing is run
# default k =0.1 and iterations=50

注意,我刚刚从Stack上的另一个问题中找到了这些参数,您可以在这里找到:How to increase node spacing for networkx.spring_layout

让我知道这是否有帮助。

答案 1 :(得分:0)

问题是你有非常高密度的集群。然后布局算法很难使每个边缘完全清晰。创建一个好的布局可能很困难。

我建议你试试Graphviz,它提供了非常好的布局调整,可以提供你想要的布局。您可以使用networkx.draw_graphviz从Networkx调用Graphviz。

关于最终布局的随机性,这是由于初始布局通常在布局算法中随机化。我不知道是否有办法在Networkx中播种随机化。

关于边缘数量减少的第三次编辑,我没有看到问题。

import networkx as nx
edges = [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [1, 10], [1, 11], [1, 12], [1, 13], [1, 14], [1, 15], [16, 17], [16, 18], [16, 19], [16, 20], [16, 21], [16, 22], [16, 23], [16, 24], [16, 25], [16, 26], [16, 27], [16, 28], [16, 29], [16, 30], [1, 16], [2, 17], [2, 3], [5, 6], [8, 9], [9, 4], [18, 26], [17, 22], [29, 21], [17, 28]]
g = nx.Graph(edges)
nx.draw_networkx(g)

产地:

Example of graph layout

根据我的说法,这似乎很清楚。