改变 Networkx lib 中边的长度

Changing length of the edges in Networkx lib

我几天前开始使用 networkx 库。我想知道是否可以更改图形上边的长度? 我绘制了一个图表,但节点彼此非常接近,因此节点名称重叠(检查下图)。 这是我的代码:

import networkx as nx
import matplotlib.pyplot as plt

# Defining graph .Graph() and .DiGraph()
analysis_graph = nx.DiGraph()


# Adding relations to the graph
analysis_graph.add_edges_from(relation_list)


# extracting nodes from relations - Unique node entities
node_list = list(nx.nodes(analysis_graph))
print(type(node_list))


# Creating sizes for each node (degree - number of relations from each node)
dict_of_node_sizes = dict(analysis_graph.degree) # for getting node sizes
print(dict_of_node_sizes)


# Same graph each time
my_pos = nx.spring_layout(analysis_graph.to_undirected(), seed = 0)
#.to_undirected() -> Making shape of directed graph like undirected graph


# Printing graph info
print(nx.info(analysis_graph))


# Printing graph
plt.figure(figsize=(25,17))
nx.draw(analysis_graph, 
        pos = my_pos, 
        with_labels = True, 
        arrowsize=10, 
        font_size=10, 
        node_size=[(v+1) * 120 for v in dict_of_node_sizes.values()])

这是我的图表:

你知道我怎样才能修正图表的外观,使点头清晰可见吗? 我应该制作更长的边缘(如何)还是应该更改字体或其他?

您的主要问题是节点和标签重叠。由于两者都是深色,因此都看不清楚。然而,节点布局实际上看起来相当不错,因为它突出了图形的中心结构。所以我不会将边长更改为 space 节点更远。

在我看来,您的第一个选择是不强调节点和边缘,这样标签就更容易阅读了。这可以通过使节点和边缘颜色更浅来实现。

您的第二个选择是从节点偏移节点标签。但是,这很重要,因为您需要防止节点标签与其他标签、其他节点和图形的边缘重叠。不幸的是,there isn't any functionality within networkx that reduces node label overlaps. However, to deal with this and other issues, I wrote netgraph,它是一个网络可视化库并与 networkx 图兼容。 概述了我避免节点标签重叠的方法 here。但是,如果您告诉 netgraph 在标量偏移处绘制标签,它就会这样做,因此您不需要做任何特别的事情。

#!/usr/bin/env python
import random
import string
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx

from netgraph import Graph # pip install netgraph

# create random graph with hubs and leafs
hubs = np.random.randint(5, 15, size=10)
leafs = np.ones((np.sum(hubs)), dtype=int)
degrees = np.concatenate([hubs, leafs])
g = nx.configuration_model(degrees, create_using=nx.Graph)
giant_component = next(nx.connected_components(g))
h = g.subgraph(giant_component)

# generate random labels
def random_string(length):
    # 
    letters = string.ascii_lowercase
    return ''.join(random.choice(letters) for ii in range(length))

labels = {node : random_string(5) for node in h}

# generate node sizes
node_size = dict(g.degree)
nx_node_size = np.array([100*node_size[node] for node in h])
ng_node_size = {node : np.sqrt(size) for node, size in node_size.items()}

# same positions for comparability
node_layout = nx.spring_layout(h)

# plot
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 12))
nx.draw(h, pos=node_layout, labels=labels, node_size=nx_node_size, node_color='lightgray', edge_color='darkgray', ax=ax1)
Graph(h, node_layout=node_layout, node_labels=labels, node_label_offset=0.1, node_size=ng_node_size, ax=ax2)
plt.show()