在python中将点转换为png

时间:2011-03-15 18:20:50

标签: python

我有一个从我的代码生成的点文件,并希望在我的输出中呈现它。为此,我在网上看到命令在cmd

上是这样的
dot -Tpng InputFile.dot -o OutputFile.png  for Graphviz

但我的问题是我想在我的python程序中使用它。

我该怎么办?

我看着pydot但似乎无法在那里找到答案.....

7 个答案:

答案 0 :(得分:40)

使用pydot.graph_from_dot_file加载文件以获取pydot.Dot类实例。然后使用write_png方法将其写入PNG文件。

import pydot

(graph,) = pydot.graph_from_dot_file('somefile.dot')
graph.write_png('somefile.png')

答案 1 :(得分:19)

pydot无论如何都需要安装GraphViz二进制文件,所以如果你已经生成了你的点文件,你也可以直接自己调用点。例如:

from subprocess import check_call
check_call(['dot','-Tpng','InputFile.dot','-o','OutputFile.png'])

答案 2 :(得分:2)

您可以使用pygraphviz。加载图表后,即可

graph.draw('file.png')

答案 3 :(得分:2)

您可以尝试:

import os
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/'
os.system('dot -Tpng random.dot -o random.png')

答案 4 :(得分:2)

您可以使用graphviz

# Convert a .dot file to .png
from graphviz import render
render('dot', 'png', 'fname.dot')

# To render an existing file in a notebook
from graphviz import Source
Source.from_file("fname.dot")

答案 5 :(得分:1)

第一个解决方案)

使用@Mauricio Carrero的方法通过在脚本内设置PATH(在环境变量中设置相同的PATH不会产生这种作用!)来实现:

import os
import pydotplus
from sklearn.tree import export_graphviz

os.environ['PATH'] = os.environ['PATH']+';' + r'C:\Users\Admin\Anaconda3\Library\bin\graphviz'

# first export the dot file only if needed
export_graphviz(clf, out_file=filename + ".dot", feature_names = feature_names)
# now generate the dot_data again
dot_data = export_graphviz(clf, out_file=None, feature_names = feature_names)
graph = pydotplus.graphviz.graph_from_dot_data(dot_data)
graph.write_png(filename + "_gv.png")

这使得可以将dot_data保存为png。选择自己的本地路径,您可能还已经在`C:/ Program Files(x86)/Graphviz2.38/bin /

中安装了graphviz

此解决方案也来自Sarunas的回答: https://datascience.stackexchange.com/questions/37428/graphviz-not-working-when-imported-inside-pydotplus-graphvizs-executables-not

第二个解决方案)

您还可以避免错误

Exception has occurred: InvocationException
GraphViz's executables not found

通过简单地提供它想要的内容,因为它需要graphviz对象的可执行文件:

graph = pydotplus.graphviz.graph_from_dot_data(dot_data)
# graph is now a new Dot object!
# That is why we need to set_graphviz_executables for every new instance
# This cannot be set globally but must be set again and again
# that is why the PATH solution (1st Solution) above seems much better to me
# see the docs in https://pydotplus.readthedocs.io/reference.html
pathCur = 'C:\\Program Files (x86)\\Graphviz2.38\\bin\\'
graph.set_graphviz_executables({'dot': pathCur+'dot.exe', 'twopi': pathCur +'twopi.exe', 'neato': pathCur+'neato.exe', 'circo': pathCur+'circo.exe', 'fdp': pathCur+'fdp.exe'})
graph.write_png(filename + "_gv.png")

p.s: 这2种方法是在校准erroneuos安装并完全卸载并重新安装2小时,所有种类的PATH变量,外部和内部graphviz安装,python-graphviz,pygraphviz以及我可以在中找到的所有解决方案之后的两个小时,对我而言唯一的解决方案在这里或在 Convert decision tree directly to png 或在 https://datascience.stackexchange.com/questions/37428/graphviz-not-working-when-imported-inside-pydotplus-graphvizs-executables-not?newreg=a789aadc5d4b4975949afadd3919fe55

对于conda python-graphviz,我经常遇到类似

的安装错误
InvalidArchiveError('Error with archive C:\\Users\\Admin\\Anaconda3\\pkgs\\openssl-1.1.1d-he774522_20ffr2kor\\pkg-openssl-1.1.1d-he774522_2.tar.zst.  You probably need to delete and re-download or re-create this file.  Message from libarchive was:\n\nCould not unlink')

对于conda安装graphviz,我得到了

InvalidArchiveError('Error with archive C:\\Users\\Admin\\Anaconda3\\pkgs\\openssl-1.1.1d-he774522_21ww0bpcs\\pkg-openssl-1.1.1d-he774522_2.tar.zst.  You probably need to delete and re-download or re-create this file.  Message from libarchive was:\n\nCould not unlink')

pygraphviz需要我不想安装的MS Visual C ++:

error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/

最后,除第一种解决方案方法外,没有任何指南可以真正正确设置PATH变量。

答案 6 :(得分:0)

在python中将点转换为png的最简单方法是:

从graphviz导入渲染

render('dot','png','filename.dot')

答案是受Sam Perry启发