25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3.6 KiB

graphviz: tale to tell

pip3 install graphviz
apt install graphviz

The graphviz library can be directly used with its commandline commands (like dot, neato, etc.)

In [ ]:
from graphviz import Digraph
In [ ]:
dot = Digraph()
In [ ]:
# Add nodes 1 and 2
dot.node('1')
dot.node('2')

# Add edge between 1 and 2
dot.edges(['12'])
In [ ]:
dot
In [ ]:
text = """I have a tale to tell
Sometimes it gets so hard to hide it well
I was not ready for the fall
Too blind to see the writing on the wall"""
In [ ]:
for word in text.split():
    print (word)
In [ ]:
from itertools import tee
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)
In [ ]:
for w1, w2 in pairwise(text.split()):
    print (w1, w2)
In [ ]:
for w1, w2 in pairwise(text.split()):
    dot.edge(w1, w2)
In [ ]:
dot
In [ ]:
dot.render("tale")
In [ ]:
dot.render?
In [ ]:
# Example of using the graph as a generator ??
In [ ]:
dot.render("tale", format="png")
In [ ]: