def draw_tree(tree, path): dot_data = StringIO() dot_data.seek(0) # to reset graph? dot_data.truncate(0) # to reset graph? export_graphviz(tree, out_file=dot_data, feature_names=tree.feature_names, filled=True, impurity=False, proportion=True, rounded=True, special_characters=True, node_ids=True) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) open(path, 'wb').write(Image(graph.create_png()).data)