leon yin
leon yin

Reputation: 849

Graphviz.Source not rendering in Jupyter Notebook

After exporting a .dot file using scikit-learn's handy export_graphviz function.

I am trying to render the dot file using Graphviz into a cell in my Jupyter Notebook:

import graphviz
from IPython.display import display

with open("tree_1.dot") as f:
    dot_graph = f.read()
display(graphviz.Source(dot_graph))

However the out[ ] is just an empty cell.

I am using graphviz 0.5 (pip then conda installed), iPython 5.1, and Python 3.5 The dot file looks correct here are the first characters:

digraph Tree {\nnode [shape=box, style="filled", color=

iPython display seems to work for other objects including Matplotlib plots and Pandas dataframes.

I should note the example on Graphviz' site also doesn't work.

Upvotes: 18

Views: 32883

Answers (5)

aMaklad
aMaklad

Reputation: 41

this solution allows you to insert DOT text directly (without saving it to file first)

# convert a DOT source into graph directly
import graphviz 
from IPython.display import display
    
source= '''\
digraph sample {
A[label="AL"]
B[label="BL"]
C[label="CL"]
A->B
B->C 
B->D
D->C
C->A
}
'''
print (source)
gvz=graphviz.Source(source)
# produce PDF
#gvz.view()
print (gvz.source)
display(gvz)

Upvotes: 4

Andy Quiroz
Andy Quiroz

Reputation: 901

try to reinstall graphviz

conda remove graphviz
conda install python-graphviz
graphviz.Source(dot_graph).view()

Upvotes: 2

martin.zaenker
martin.zaenker

Reputation: 252

Try to use pydotplus.

import pydotplus

by (1.1) Importing the .dot from outside

pydot_graph = pydotplus.graph_from_dot_file("clf.dot")

or (1.2) Directly using the .export_graphviz output

dt = tree.DecisionTreeClassifier()
dt = clf.fit(x,y)
dt_graphviz = tree.export_graphviz(dt, out_file = None)

pydot_graph = pydotplus.graph_from_dot_data(dt_graphviz)

(2.) and than display the pyplot graph using

from IPython.display import Image

Image(pydot_graph.create_png())

Upvotes: 2

Aaron Soellinger
Aaron Soellinger

Reputation: 327

It's possible that since you posted this, changes were made so you might want to update your libraries if that's possible.

The versions of relevance here I used are:

Python 2.7.10

IPython 5.1.0

graphviz 0.7.1

If you have a well formed .dot file, you can display it to the jupyter out[.] cell by the following:

import graphviz

with open("tree_1.dot") as f:
    dot_graph = f.read()

# remove the display(...)

graphviz.Source(dot_graph)

Upvotes: 14

kapfy78
kapfy78

Reputation: 43

graphviz.Source(dot_graph).view()

Upvotes: 0

Related Questions