在python中将点转换为png
我有一个从我的代码生成的点文件,并希望在我的输出中呈现它。为此,我在网上看到命令类似于 cmd
dot -Tpng InputFile.dot -o OutputFile.png for Graphviz
但我的问题是我想在我的 python 程序中使用这个内置命令。
我该怎么办?
我查看了 pydot 但似乎无法在那里找到答案......
I have a dot file generated from my code and want to render it in my output. For this i have seen on the net that the command is something like this on cmd
dot -Tpng InputFile.dot -o OutputFile.png for Graphviz
But my problem is that I want to use this inbuilt in my python program.
How can i do so ??
I looked at pydot but can't seem to find an answer in there.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用 pydot.graph_from_dot_file 加载文件以获取 pydot.Dot 类实例。然后使用
write_png
方法将其写入 PNG 文件。Load the file with
pydot.graph_from_dot_file
to get apydot.Dot
class instance. Then write it to a PNG file with thewrite_png
method.pydot 无论如何都需要安装 GraphViz 二进制文件,所以如果您已经生成了点文件您也可以自己直接调用 dot 。例如:
pydot needs the GraphViz binaries to be installed anyway, so if you've already generated your dot file you might as well just invoke dot directly yourself. For example:
您可以使用pygraphviz。加载图表后,您可以执行以下操作
You can use pygraphviz. Once you have a graph loaded, you can do
您可以使用 graphviz:
You can use graphviz:
您可以尝试:
You can try:
第一个解决方案)
通过在脚本内设置 PATH 进一步采用 @Mauricio Carrero 的方法(在环境变量中设置的相同 PATH 没有此效果!):
这使得将 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
第二个解决方案)
您还可以通过简单地提供它想要的东西来避免错误
,因为它要求可执行文件graphviz 对象的:
ps:
经过 2 个小时的校准错误安装、完全卸载并再次安装、各种 PATH 变量、外部和内部 graphviz 安装、python-graphviz、pygraphviz 以及我可以找到的所有解决方案之后,这两种方法是唯一对我有用的解决方案在这里,或者在
将决策树直接转换为 png
或在
https://datascience。 stackexchange.com/questions/37428/graphviz-not-working-when-imported-inside-pydotplus-graphvizs-executables-not?newreg=a789aadc5d4b4975949afadd3919fe55
对于 conda python-graphviz,我遇到了持续的安装错误,例如
For conda install graphviz,我得到
pygraphviz 需要 MS Visual C++,但我不想安装:
最后,除了第一个解决方案方法之外,没有指南能够真正正确地设置 PATH 变量。
1st Solution)
Going further with the approach of @Mauricio Carrero by setting the PATH inside the script (the same PATH set in the environment variables does not have this effect!):
This made it possible to save the dot_data to png. Choose your own local paths, you might also have installed graphviz in `C:/Program Files (x86)/Graphviz2.38/bin/
This solution also came from Sarunas answer here:
https://datascience.stackexchange.com/questions/37428/graphviz-not-working-when-imported-inside-pydotplus-graphvizs-executables-not
2nd Solution)
You can also avoid the error
by simply giving it what it wants, as it asks for the executables of the graphviz object:
p.s:
These 2 approaches were the only solutions working for me after 2 hours of calibrating erroneuos installations and full uninstall and install again, all varieties of PATH variables, external and internal graphviz installation, python-graphviz, pygraphviz and all of the solutions I could find in here, or in
Convert decision tree directly to png
or in
https://datascience.stackexchange.com/questions/37428/graphviz-not-working-when-imported-inside-pydotplus-graphvizs-executables-not?newreg=a789aadc5d4b4975949afadd3919fe55
For conda python-graphviz, I got constant installation errors like
For conda install graphviz, I got
pygraphviz needs MS Visual C++ which I did not want to install:
In the end, no guide was working to really set the PATH variables correctly except for the 1st Solution approach.
这是使用 venv python 3.8 解释器使用 pycharm 终端在 Windows 11 系统上为我工作的代码。
这将创建一个classes.dot.pg文件(我不知道如何修复名称,但这是一个可以打开的png文件)
类点是在终端上生成的
与 pylint 一起提供。
安装:
(仅当您创建classes.dot 文件时才安装 pylint)
Here is the code that worked for me on windows 11 system using a pycharm terminal using venv python 3.8 interpreter.
this will create a classes.dot.pg file (I don't know how to fix the name but this is a png file you can open)
The classes dot was generated on terminal using
pyreverse comes with pylint.
Installations:
(install pylint only if you are creating classes.dot file)
这将创建一个图表“a”到“b”并将其保存为 png 文件。
代码:
This would create a graph 'a' to 'b' and save it as a png file.
code: