Reputation: 133
I'm doing a project where i am drawing a graph from a python repo, my code is really long, and not the important part. But here it is, in case someone wants to try to reporduce:
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, height="1500px", width="100%")
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")
Now, when i draw my graph, it looks like this, things are super bunched up on top of eachother. So I wanted some way to avoid overlapping. After researching a bit, I found that I should probably add this line:
net.barnes_hut(overlap=1)
Which, I do. This results in this, which looks much nicer, but now all of the sudden, all the labels on the nodes are gone!?.
Why is my labels gone? and how can I get both non-overlapping, and labels? Edit:
I now tried this option:
net.force_atlas_2based(overlap= 1)
and this actually does what I want. The only problem now is that the nodes sometimes hit eachother, and start wiggling uncontrollably
Upvotes: 0
Views: 3588
Reputation: 31
Try using option menu and play with different gravity features of force_atlas_2based()
(i.e., gravitationalConstant
and centralGravity
).
You can have the option menu in your saved .html file:
# Graph construction ...
# ...
# Export to HTML file with physics manipulation options
net.show_buttons(filter_=["physics"])
net.show("graph_test.html")
Upvotes: 3