python - 如何在 networkx python 包中明显地可视化密集图?

标签 python graph networkx graph-visualization

我在 python 中有一个大的、密集的有向图,用 NetworkX 制作包裹。如何提高图形图像的清晰度?

下图显示了我的图表。
enter image description here

最佳答案

我可以根据其大小向您推荐几种改进图形可视化的方法。

如果你想可视化一个大图(>1000 个节点),你可以在 my another answer 中阅读一些技巧。 .在您的情况下,我建议您将图形导入大矢量图:

import networkx as nx 
import matplotlib.pyplot as plt 
fig = plt.figure(figsize=(40, 40)) 
G = nx.fast_gnp_random_graph(300, 0.02, seed=1337) 
nx.draw(G, node_size=30) 
plt.axis('equal') 
plt.show() 
fig.savefig('waka.svg') 


如果您的图形相对较小(<1000 个节点),您可以使用图形布局。

最适合您的图表类型的布局是默认 spring_layout .它有 k参数设置节点之间的最佳距离。这是示例:

默认 k 值
import networkx as nx
import random
random.seed(1234)
G = nx.fast_gnp_random_graph(30, 0.4, seed=1337)
for i in range(20):
    G.add_edge(i + 40, random.randint(1, 30))
    G.add_edge(i + 40, random.randint(1, 30))
pos = nx.spring_layout(G, seed=4321)
nx.draw(G, pos=pos, node_size=30, node_color='red')

enter image description here

放大k值
import networkx as nx
import random
random.seed(1234)
G = nx.fast_gnp_random_graph(30, 0.4, seed=1337)
for i in range(20):
    G.add_edge(i + 40, random.randint(1, 30))
    G.add_edge(i + 40, random.randint(1, 30))
pos = nx.spring_layout(G, seed=4321, k=2)
nx.draw(G, pos=pos, node_size=30, node_color='red')

enter image description here

如果您需要以高精度分析边缘,则可读性较差,但如果您更关心节点,则更好。

关于python - 如何在 networkx python 包中明显地可视化密集图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57696572/

相关文章:

algorithm - VF2算法步骤举例

algorithm - 有两个条件的最短路径问题

Python 多处理池 async_apply 回调在传递参数时不起作用

python - Matplotlib 颜色根据类标签

java - Python/C++/Java : Is there a library that will recognize characters and tell me where they are in an image?

python - 从字典绘制直方图时出错

python - 创建 "minimally connected"有向无环图

python - 使用 matplotlib 更改绘图中特定刻度的颜色

javascript - 如何更改 Recharts 中每个条的颜色?

macos - easy_install networkx