xflr6 / graphviz

Simple Python interface for Graphviz
https://graphviz.readthedocs.io
MIT License
1.59k stars 209 forks source link

Render with transparent background? #195

Closed galenseilis closed 1 year ago

galenseilis commented 1 year ago

For my blog I would like to render graphviz diagrams in PNG format of models I've trained in PyMc. Is there a way to make the background canvas transparent?

Here is an example from pymc

import numpy as np
from pymc import HalfCauchy, Model, Normal, model_to_graphviz

J = 8
y = np.array([28, 8, -3, 7, -1, 1, 18, 12])
sigma = np.array([15, 10, 16, 11, 9, 11, 10, 18])

with Model() as schools:

    eta = Normal("eta", 0, 1, shape=J)
    mu = Normal("mu", 0, sigma=1e6)
    tau = HalfCauchy("tau", 25)

    theta = mu + tau * eta

    obs = Normal("obs", theta, sigma=sigma, observed=y)

model_to_graphviz(schools)
xflr6 commented 1 year ago

I don't know pymc, but from the API I understand you can modify the result of model_to_graphviz() (a graphviz.Digraph). See https://graphviz.org/doc/info/attrs.html#d:bgcolor (I did not verify the transparency):

import graphviz

dot = graphviz.Digraph()

dot.edge('hello', 'world')
dot

spam1

dot.attr(bgcolor='transparent')
dot

spam2

dot.attr(bgcolor='green')
dot

spam3