首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Networkx:图形标签混淆且未调整

Networkx:图形标签混淆且未调整
EN

Stack Overflow用户
提问于 2019-06-27 07:43:07
回答 1查看 944关注 0票数 1

所以我使用networkx库制作了我的图,这是一个单部图。当我贴上标签的时候,它看起来不太对劲,它弄混了。有一些词的长度比其他词长,并且超出了节点的边界。我的图表是否可以调整,让一切看起来都清晰易懂?

我还希望节点像一个点,标签出现在节点上,而不是节点内部。

像这样

我做的图表..

这是代码..

代码语言:javascript
复制
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
from networkx.algorithms import community
from networkx.algorithms import bipartite


G1 = nx.read_edgelist("abcd.txt")
print(nx.info(G1))
c = bipartite.color(G1)
nx.set_node_attributes(G1, c, 'bipartite')
type1  = {n for n, d in G1.nodes(data=True) if d['bipartite']==0}
type2  = {n for n, d in G1.nodes(data=True) if d['bipartite']==1}
G = bipartite.projected_graph(G1, type1)
type2g = bipartite.projected_graph(G1, type2)


pos = nx.spring_layout(G,k=0.30,iterations=50)

nx.is_bipartite(G1)
#average clustering
nx.average_clustering(G1)

#Diameter
print("Diameter:",nx.diameter(G1))

options = {
    'node_color': 'purple',
    'node_size': 40,
    'line_color': 'yellow',
    'linewidths': 0,
    'width': 0.3,
}

#degeree plotting
def plot_degree_distribution(wiki):
    degs = {}
    for n in wiki.nodes():
        deg = wiki.degree(n)

        if deg not in degs:
            degs[deg] = 0

        degs[deg] += 1
    items = sorted(degs.items())

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot([k for (k, v) in items], [v for (k, v) in items])
    ax.set_xscale('log')
    ax.set_yscale('log')
    plt.title("Degree Distribution")
    fig.savefig("degree_distribution.png")


# plot_degree_distribution(G)
d = []  # create a set
for n in G.nodes():
    d.append(G.degree(n))

ec = []  # create a set
for e in G.edges():
    if (G.degree(e[0]) > G.degree(e[1])):
        ec.append(G.degree(e[0]))
    else:
        ec.append(G.degree(e[1]))

pos = nx.spring_layout(G,k=1.5, iterations=200)

factor = 25  # to change the size of nodes with respect to their degree


nx.draw_networkx(G, pos,
        edge_color=ec, edge_cmap=plt.cm.plasma,  # edge color
        node_color=d, cmap=plt.cm.plasma,  # node color
        node_size=[x * factor for x in d])  # node sizse
plt.savefig ("simple_graph.png")
fig = plt.gcf()
fig.set_size_inches((10,10))
plt.show()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-27 09:50:44

将弹簧布局更改为圆形布局将有助于您获得更好的标签可视化效果。

代码语言:javascript
复制
cgraph=nx.gnp_random_graph(15,p=0.5)
labels={}
for k in range(15):
    labels[k]='long named node '+str(k)

circPos=nx.circular_layout(cgraph)
nx.draw_networkx_labels(cgraph,pos=circPos,labels=labels) 
nx.draw(cgraph,pos=circPos)

此外,如果边缘标签难以可视化,则可以通过更改布局位置来更改标签的位置,如下所示。

代码语言:javascript
复制
pos_attrs = {}
for node, coords in circPos.items():
    pos_attrs[node] = (coords[0]+0.1*(-1)*np.sign(coords[0]), coords[1]+0.1*(-1)*np.sign(coords[1]))
nx.draw_networkx_labels(cgraph,pos=circPos,labels=labels) 
nx.draw(cgraph,pos=pos_attrs)

这里介绍了在圆形布局上处理标签的另一种很好的方法:NetworkX node labels relative position

希望能有所帮助

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56782214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档