【问题标题】:Changing length of the edges in Networkx lib在 Networkx lib 中更改边的长度
【发布时间】:2021-09-27 07:48:26
【问题描述】:

几天前我开始使用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()])

这是我的图表:

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

【问题讨论】:

  • analysis_graph.add_edge('A', 'B', length = 1)
  • analysis_graph.add_edges_from([(1, 2), (2, 3)], weight=3)
  • 它不起作用,它不会改变
  • 我将列表传递给'analysis_graph.add_edges_from(relation_list)',我尝试添加length = 1,length = 10,但结果是一样的

标签: python plot networkx


【解决方案1】:

您的主要问题是节点和标签重叠。由于两者都是深色,因此两者都不是清晰可见的。然而,节点布局实际上似乎相当不错,因为它突出了图的中心结构。所以我不会改变边缘长度来将节点间隔得更远。

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

您的第二个选项是从节点偏移节点标签。但是,这并非易事,因为您需要防止节点标签与其他标签、其他节点和图形边缘重叠。不幸的是,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()

【讨论】:

  • 你能逐行解释你做了什么吗?答案是惊人的!
  • 哪一部分不明白?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-22
  • 1970-01-01
  • 2019-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多