几天前,我开始使用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()])
这是我的图表:
你知道我如何修复图形的外观,使节点清晰可见吗?我应该做更长的边缘(如何),或者我应该改变字体,或其他什么?
发布于 2021-09-30 11:49:22
您的主要问题是节点和标签重叠。因为它们都是深色的,所以都看不清楚。然而,节点布局实际上看起来相当不错,因为它突出了图形的中心结构。因此,我不会改变边的长度来使节点间隔得更远。
在我看来,您的第一个选择是去强调节点和边,这样标签就变得更容易阅读。这可以通过使节点和边的颜色变浅来实现。
您的第二个选择是从节点偏移节点标签。但是,这不是微不足道的,因为您需要防止节点标签与其他标签、其他节点和图的边重叠。不幸的是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()
https://stackoverflow.com/questions/69342952
复制相似问题