pyvis 图形重叠或不显示标签

Posted

技术标签:

【中文标题】pyvis 图形重叠或不显示标签【英文标题】:pyvis graph either overlapping, or not showing labels 【发布时间】:2021-07-27 05:19:19 【问题描述】:

我正在做一个项目,我从 python 存储库中绘制图表,我的代码真的很长,而不是重要的部分。但这里是,以防有人想尝试重现:

import ast
from radon.visitors import ComplexityVisitor
import re
import os
from pyvis.network import Network


class Vert:
    def __init__(self, name, id, size ,edges):
        self.name = name
        self.size = size
        self.edges = edges
        self.id = id
        
from pathlib import Path

rootDir = "/home/ask/Git/Zeeguu-API/"
directories = set()
# this is horrible
for file in Path(rootDir).rglob("*.py"):
    localDirs = str(file).split('/')
    directories.add(localDirs[-2])


def extract_importandClass_from_line(unline):

    x = re.search("^import (\S+)", unline) 
    x = re.search("^from (\S+)", unline) 
    return x.group(1)#, c.group(1).split('(')[0]
def extractClass(inline):
    c = re.search("^class (\S+)", inline) 
    return c.group(1).split('(')[0]


def importsAndClass(file):
    lines = [line for line in open(file)]
    classes = []
    all_imports = []
    for line in lines:
        try:
            imports = extract_importandClass_from_line(line)
            importEnd = imports.rsplit('.',1)[-1]
            importsFormatted = imports.replace('.', '/')
            if (importEnd not in directories):
                all_imports.append(importsFormatted)
        except:
            try:
                class1 = extractClass(line)
                classes.append(class1)
            except:
                continue  
  
    return all_imports, classes
net = Network(directed=True, , )
nodes = 
nodeNames = set()
counter = 0
for file in Path(rootDir).rglob("*.py"):
    # Opening file, and looking at contents
    f = open(file, "r")
    s = f.read()
    # analyzing complexity
    filename = str(file).replace(rootDir, "")
    analyzer = ComplexityVisitor.from_code(s)
    
    # getting the file name 
    splitFile = os.path.splitext(file.name)
    #getting imports    
    imports, classes = importsAndClass(file)

    nodeNames.add(str(filename))
    v = Vert(str(filename), counter,analyzer.total_complexity, imports)
    #creating vertex
    nodes[v.name] = v
    counter = counter + 1 
    net.add_node(v.id, label=v.name, size=v.size*2)

print("_________________________________")
for k, v in nodes.items():
    
    for i in v.edges:
        withPY = i + ".py"
        print(withPY)
        try:
            to = nodes[withPY].id 
            net.add_edge(v.id, to)
        except:
            print("could not add edge to:" + str(i))    
        


net.show("network.html")

现在,当我绘制我的图表时,它看起来像 this,事情超级堆积在彼此之上。 所以我想要一些方法来避免重叠。经过一番研究,我发现我应该添加这一行:

net.barnes_hut(overlap=1)

我愿意。这导致this,看起来好多了,但现在突然之间,节点上的所有标签都不见了!?。

为什么我的标签不见了?以及如何获得不重叠和标签? 编辑:

我现在尝试了这个选项:

net.force_atlas_2based(overlap= 1)

这实际上是我想要的。现在唯一的问题是节点有时会相互碰撞,并开始不受控制地摆动

【问题讨论】:

【参考方案1】:

尝试使用选项菜单并使用force_atlas_2based() 的不同重力特征(即gravitationalConstantcentralGravity)。

您可以在保存的 .html 文件中拥有选项菜单:

# Graph construction ...
# ...
# Export to HTML file with physics manipulation options
net.show_buttons(filter_=["physics"])
net.show("graph_test.html")

【讨论】:

以上是关于pyvis 图形重叠或不显示标签的主要内容,如果未能解决你的问题,请参考以下文章

显示具有动态尺寸的标签,如表格而不重叠

感谢matplotlib,如何在同一图中显示具有重叠数据的多个图形?

如何绘制两个相互重叠的图形?

Matplotlib 显示重叠的 x-tick 标签

Knightlab时间线js - 如何显示事件而不相互重叠

echart:地图上标签重叠问题解决