Search code examples
pythongraphgraphicstreegraphviz

Graphviz: how to create an edge where parts of the label have different colors


I'm not sure how to color one part of the text with one color and another part with a different color. It's probably just easier to show you what I want with a picture.

This is the best I can get it to do.

Incorrect:

incorrect

This is what I want (I edited this image in photoshop)

Correct (with photoshop):

correct

Here is some code for reference.

import graphviz

graph = graphviz.Digraph()
graph.attr(rankdir='LR')
graph.attr('node', shape='circle')

graph.node("1", label="1")
graph.node("2", label="2")

graph.edge("1", "2", label="abcdef", fontcolor="red")

display(graph)

Solution

  • Here is the answer in straight Graphviz. Create an HTML-like textitem (https://graphviz.org/doc/info/shapes.html#html)

    digraph C {
     {rank=same
       a->b [label=<a bc<font color="red">d ef </font><font color="green">g h i</font>>]
     }
    }
    

    Giving:
    enter image description here