Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting colour of nodes in pyGraphViz

This example is given on the website but there is no mention about how to set the colours using dictionaries.

import pygraphviz as pgv
d={'1': {'2': None}, '2': {'1': None, '3': None}, '3': {'2': None}}
A=pgv.AGraph(d)

Are you supposed to use get_node after initialising via AGraph ?

like image 791
BlueTrin Avatar asked Jan 26 '26 17:01

BlueTrin


1 Answers

Found out that you should use effectively get_node() and pass the id to find the node. Also found out that you need to set the style of the graph to filled or you will not see the fill colour.

import pygraphviz as pgv
from PIL import Image

d={'1': {'2': None}, '2': {'1': None, '3': None}, '3': {'2': None}}
A=pgv.AGraph(d)
A.node_attr['style']='filled'
n = A.get_node('1')
n.attr['fillcolor']="#CCCCFF"
n.attr['label'] = 'MY LOVELY LABEL'
A.layout() # default to neato
A.layout(prog='dot') # use dot
A.draw('file.png')
img = Image.open('file.png')
img.show()
like image 156
BlueTrin Avatar answered Jan 28 '26 11:01

BlueTrin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!