'graphviz: change color of some nodes

I am using NetworkX to parse a graph and graphviz to represent that same graph as an svg file. I am trying to color each node of my graph based on the name of the node such as:

if node.name == 'abc':
  node.color = 'blue'
else:
  node.color = 'pink'

I was able to color every node of my graph with the same color and I am not able to set conditions to have different node colors based on my previous algorithm. enter image description here

Based on this StackOverflow post, Graphviz dot: How to change the colour of one record in multi-record shape, it is probably not possible. But it I look at the .dot output file, each node as a color attribute so it should be possible to change it?

graph.dot:

digraph "" {
    graph [bb="0,0,61610,509.51",
        edge_default="{}",
        edges="{'arrowsize': '4.0'}",
        node_default="{}",
        rankdir=TD
    ];
    node [fillcolor=red,
        label="\N",
        shape=rectangle,
        style=filled
    ];
    "gold_adhoc_demand_review.countries"    [color=red,
        height=0.5,
        kind=adhoc,
        pos="1010,234",
        table=countries,
        use_case=demand_review,
        width=3.1389,
        zone=gold];
    "gold_core.location"    [color=blue,
        height=0.5,
        kind=core,
        pos="868,306",
        table=location,
        width=1.6389,
        zone=gold];
    "gold_core.location" -> "gold_adhoc_demand_review.countries"    [pos="e,975.13,252.19 902.74,287.88 921.82,278.47 945.68,266.71 966.05,256.67"];
    "gold_adhoc_product_priorities.distribution_network"    [color=red,
        height=0.5,
        kind=adhoc,
        pos="150,234",
        table=distribution_network,
        use_case=product_priorities,
        width=4.1667,
        zone=gold];
    "gold_core.location" -> "gold_adhoc_product_priorities.distribution_network"    [pos="e,300.22,251.11 808.82,299.62 708.56,290.46 498.65,271.06 310.31,252.13"];
    "gold_adhoc_global_supply_review.country"   [color=red,
        height=0.5,
        kind=adhoc,
        pos="1267,234",
        table=country,
        use_case=global_supply_review,
        width=3.4861,
        zone=gold];
...

draw.py

def draw_graph(networkx_graph):
    networkx_graph.graph['graph'] = {'rankdir': PYGRAPHIZ_RANKDIR}
    networkx_graph.graph['node'] = {'shape': PYGRAPHIZ_SHAPE_TYPE, 'fillcolor': 'red', 'style':'filled'}
    networkx_graph.graph['edges']={'arrowsize': PYGRAPHIZ_ARROW_SIZE}
    pygraphviz_graph = to_agraph(networkx_graph)
    pygraphviz_graph.layout("svg")
    pygraphviz_graph.draw("result.svg")
    print(pygraphviz_graph.graph_attr)


Solution 1:[1]

The property you want to update is fillcolor, not color. color controls the border of a node.

(See the documentation at https://graphviz.org/docs/attrs/fillcolor/)

Your logic would look like this to customize the fill color of individual nodes:

if node.name == 'abc':
  node.fillcolor = 'gold'
else:
  node.fillcolor = 'pink'

Note: you can use RGB color codes rather than names to give you more options, e.g.: fillcolor = "#ffd700"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steve Bogart