Saving Pymc3 pm.model_to_graphviz(model) to png

Hi, We can create Graphical model of the Model using pm.model_to_graphviz(model). How can we save the graph in png or jpg file. Thanks in advance.


I’m coming here to reflote this answer. Can someone help us with it ?

Sure, pm.model_to_graphviz(model) gives you a graphviz.Digraph object User Guide — graphviz 0.16 documentation

You can convert this to a number of formats and output using render(), e.g.

gv = pm.model_graph.model_to_graphviz(model)
gv.format = 'png'

This will output to a file called model_graph.png. Alternatively set .format to 'svg' or 'pdf' (default), and I think jpg is available. Note this also causes graphviz to dump the graph as a dict into a raw text file called model_graph next to model_graph.png.


A minor point is that the middle line isn’t needed:

gv = pm.model_to_graphviz(model)
1 Like

Which you can clear up by adding the kwarg cleanup=True into render()