2
votes

Is there a nice way to delete a node from a graph? I am using graphviz library to create nodes but they don't have a function to delete a node

Purpose: [I want to show that a node moved from a cluster to another one, but I can't find a way to delete it from the old cluster and I can't add it to the new one because of duplicate name]

I got an advice to use pydotplus library but objects from graphviz and pydotplus can't be added in one graph as a subgraph , so if you confirm there is no solution using Graphviz I have to change the whole code

1
another possible would be to use PyGraphviz where you have the delete_node function - ramin

1 Answers

1
votes

I found a way but it's not that nice,

every time you use graphviz functions you add DOT code to the graph body, which is a python list as follows:

>>> graph=gv.Digraph(format = 'gif')
>>> graph.node('a')
>>> graph.node('b')
>>> print (graph.source)
digraph {
    a
    b
} 

you can access the body list as:

>>>graph.body[0]

but the output would be like this

 '\ta'

you can delete it by two ways:

>>>del graph.body[0]

or using remove if you really know the exact format

>>> graph.body.remove('\ta')

and it's easy to know the format using

>>>graph.body[index]

then building a code to search for string match and delete them from the graph body (if you have made the subgraph for the same cluster more than one time as in my case)