我怎样画一个网络?

时间:2018-03-24 16:23:48

标签: python python-3.x matplotlib networkx

我想画一个这样的网络: enter image description here

这是我的代码:

import matplotlib
matplotlib.use('TkAgg')
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
from itertools import product
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import minimum_spanning_tree
from pymnet import*
import matplotlib.colors as mcolors

mst_pearson=mst_tree(pearson)
mst_kendall=mst_tree(kendall)
mst_tail=mst_tree(tail)

nx.draw(mst_pearson,pos=nx.spring_layout(mst_pearson))
plt.show()

mst_pearson是我自己的函数,用于生成图形的最小生成树,但我的图形喜欢这个,尽管我设置了布局: enter image description here 我怎么能改变节点和边缘的布局?提前谢谢。

1 个答案:

答案 0 :(得分:0)

使用功能

nx.draw_networkx_nodes(G, pos, [...])

您可以使用pos参数来设置特定的布局。

例如,我有这个代码段代码,以便在自定义函数

中执行此操作
def custom_draw_graph_function(graph_layout='shell'):

    # these are different layouts for the network you may try
    if(graph_layout == 'spring'):
         graph_pos=nx.spring_layout(G)
    elif(graph_layout == 'spectral'):
         graph_pos=nx.spectral_layout(G)
    elif(graph_layout == 'circular'):
         graph_pos = nx.circular_layout(G)
    elif(graph_layout == 'fruchterman'):
         graph_pos = nx.fruchterman_reingold_layout(G)
    elif(graph_layout == 'shell'):
         graph_pos = nx.shell_layout(G)
    else:
         graph_pos=nx.random_layout(G)

     # DRAW GRAPH
     # Nodes
     nx.draw_networkx_nodes(G,graph_pos,[...])

     [...]