更改Networkx库中的边的长度

2022-03-31 00:00:00 python plot networkx

问题描述

我几天前开始使用networkx lib。我想知道是否有可能改变图上边的长度? 我画了一个图,但节点之间非常接近,所以节点名称是重叠的(检查下图)。 这是我的代码:

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()])

这是我的图表:

您知道如何修复图表的外观以使节点清晰可见吗? 我应该制作更长的边缘(如何),还是应该更改字体或其他什么?


解决方案

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

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

您的第二个选择是将节点标签从节点偏移。但是,这不是一件简单的事情,因为您需要防止节点标签与其他标签、其他节点和图形的边重叠。不幸的是,there isn't any functionality within networkx that reduces node label overlaps。然而,为了处理这个和其他问题,我编写了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):
    # https://stackoverflow.com/a/2030081/2912349
    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()

相关文章