Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different colours edges in graphviz in Python

I am trying to draw graph with different colours edges by using graphviz in Python. I am creating colour list and using in edge colour. However, it looks like it does not support in graphviz. I am not sure how to set different colour of edges. Here is my code:

import graphviz as gv

d = gv.Digraph()
colors = ['green','red']
d.attr('edge', color = colors)
d.edge('hello','world')
d.edge('world','hello')
d.view()

Looking for valuable comments. Thanks

like image 667
user2293224 Avatar asked Oct 21 '25 22:10

user2293224


2 Answers

import graphviz as gv

colors = ['green','red']


def create_graph(colors, d):

  d.edge('hello','world', color=colors[0])
  d.edge('world','hello', color=colors[1])
  d.view()

if __name__ == '__main__':

  d = gv.Digraph()

  create_graph(colors, d)
like image 111
Nirav Patel Avatar answered Oct 23 '25 13:10

Nirav Patel


I don't really know the python wrapper for graphviz,but if by different colour of edges you mean multiple colors (?), you may try the following:

d.attr('edge', color = 'green:red')

Otherwise, if you'd like to have a green and a red edge, the following may work:

d.edge('hello','world', color='green' )
d.edge('world','hello', color='red' )
like image 41
marapet Avatar answered Oct 23 '25 13:10

marapet