【问题标题】:Errors in nodes size and color when drawing large graphs with matplotlib使用 matplotlib 绘制大图时节点大小和颜色错误
【发布时间】:2014-03-16 20:43:35
【问题描述】:

我正在使用 networkx 绘制大图,当图很大时我遇到了问题。对于小图,一切正常,但对于大图(1k 个节点),它似乎无法分配颜色和大小。 这是有问题的代码段

H=nx.connected_component_subgraphs(G)[0]
d=nx.degree(H)
b=nx.eigenvector_centrality(H)
pos2=nx.spring_layout(H,scale=2)

labels={}
colors={}
dim={}

for n in H.nodes():
    i=0 
    for v in b:
        if v==n:
            break
        i+=1
    colors[n]=b.values()[i]
    j=0
    for w in d:
        if w==n:
            break
        j+=1
    dim[n]=d.values()[j]*40

    k=0
    for z in range(2):
        if n in sorted(b.items(), key=lambda x:x[1],reverse=True)[z]:
            labels[n]=n


nx.draw(H,
             pos2,
             with_labels=False,
             node_size=dim.values(),
             node_color=colors.values(),
             cmap=plt.cm.Reds,
             vmin=min(b.values()),
             vmax=max(b.values())   
)

我无法发布图片,因为我没有足够的声誉,但是当我说它失败时,我的意思是(某些)连接性低的节点比连接性高的节点大,颜色也是如此。

知道发生了什么吗?

【问题讨论】:

标签: colors matplotlib size networkx


【解决方案1】:

将关键字 nodelist= 与您要绘制的节点一起传递。然后将 node_color 和 node_size 参数设置为具有相同顺序的列表中的颜色和大小。

nodes = H.nodes()

nx.draw(H, pos2, nodelist=nodes,
        node_size = [dim[n] for n in nodes],
        node_color= [colors[n] for n in nodes],
        cmap=plt.cm.Reds,
        vmin=min(b.values()),
        vmax=max(b.values()),
        with_labels=False
)

【讨论】:

    猜你喜欢
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    相关资源
    最近更新 更多