I'm currently trying to represent a graph with Graphviz, in which the edges have 2 colors. Ideally, I'd like to have each edge cut in two, one half colored in each color.
Another constraint is that I have to use the RGB encoding, like in color="#4a52ff"
. I can't use the color="black"
way.
I've tried to separe the color tags with a :
, but the result I obtain is a double edge.
Here is a minimal example :
import graphviz as gv
color1, color2 = "#3f7f3f", "#ff7f3f"
g = gv.Graph(format="png")
g.node("1", color=color1)
g.node("2", color=color2)
g.edge("1", "2", color="%s:%s" % (color1, color2))
g.render("tmp", view=True)
I obtain a small graph with 2 nodes and 1 edge, but the single edge seems to have been doubled, one version in each color. What I'd like to have is one orange end, connected to the orange node, and one green end connected to the green one.
From the Node, Edge and Graph Attributes - color page, Graphviz supports the coloring of edges if you specify a fraction.
This supports the common case of drawing opposing edges, but using parallel splines instead of separately routed multiedges. If any fraction is used, the colors are drawn in series, with each color being given roughly its specified fraction of the edge.
In your case change the following line:
g.edge("1", "2", color="%s:%s" % (color1, color2))
to:
g.edge("1", "2", color="%s;%f:%s" % (color1, 0.5, color2))
And it renders like this: