mahmood 16cs187
mahmood 16cs187

Reputation: 41

How to download the image created using graphviz

I have used Decision Tree to solve a problem. Then I have used graphviz to obtain the pictorial version of the decision tree.

import graphviz 
dot_data = tree.export_graphviz(clf, out_file=None) 
graph = graphviz.Source(dot_data) 
dot_data = tree.export_graphviz(clf, out_file=None, 
                            feature_names=f_name,   
                            class_names=['Topper', 'Not a topper'],  
                            filled=True, rounded=True,   
                            special_characters=True)  
graph = graphviz.Source(dot_data)
graph 

I wish to download this tree generated in .png, .jpg, or any suitable format. Is it possible?

Upvotes: 4

Views: 10411

Answers (3)

user25413153
user25413153

Reputation: 1

dot.render('/mnt/data/fluxograma_sac_logistico', format='png', cleanup=False) '/mnt/data/fluxograma_sac_logistico.png'

Upvotes: -1

Vijay
Vijay

Reputation: 187

From digraph source create an image

digraph source - 
graph_data = "digraph G { ...... }"

fie_ext = 'png'
temp_img = 'temp_file'
my_graph = graphviz.Source(graph_data)
my_graph.render(temp_img,format=fie_ext, view=False)

render() method auto generate an image with temp_file.png extension in your current directory.

Upvotes: 5

PT Huynh
PT Huynh

Reputation: 595

To export dot file to image, you need module pydotplus.

from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO  
import pydotplus

dot_data = StringIO()
export_graphviz(clf, out_file=dot_data,  
                filled=True, rounded=True,
                special_characters=True,feature_names = feature_cols,class_names=['0','1'])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
graph.write_png('my_decision_tree.png')

Upvotes: 3

Related Questions