在python中将点转换为png

发布于 2024-10-22 13:39:59 字数 242 浏览 2 评论 0原文

我有一个从我的代码生成的点文件,并希望在我的输出中呈现它。为此,我在网上看到命令类似于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

诺曦 2024-10-29 13:39:59

使用 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')

Load the file with pydot.graph_from_dot_file to get a pydot.Dot class instance. Then write it to a PNG file with the write_png method.

import pydot

(graph,) = pydot.graph_from_dot_file('somefile.dot')
graph.write_png('somefile.png')
執念 2024-10-29 13:39:59

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

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

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:

from subprocess import check_call
check_call(['dot','-Tpng','InputFile.dot','-o','OutputFile.png'])
魂归处 2024-10-29 13:39:59

您可以使用pygraphviz。加载图表后,您可以执行以下操作

graph.draw('file.png')

You can use pygraphviz. Once you have a graph loaded, you can do

graph.draw('file.png')
╰つ倒转 2024-10-29 13:39:59

您可以使用 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")

You can use 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")
伴梦长久 2024-10-29 13:39:59

您可以尝试:

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

You can try:

import os
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/'
os.system('dot -Tpng random.dot -o random.png')
日记撕了你也走了 2024-10-29 13:39:59

第一个解决方案)

通过在脚本内设置 PATH 进一步采用 @Mauricio Carrero 的方法(在环境变量中设置的相同 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")

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,我遇到了持续的安装错误,例如

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')

For conda install 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 变量。

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!):

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")

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

Exception has occurred: InvocationException
GraphViz's executables not found

by simply giving it what it wants, as it asks for the executables of the graphviz object:

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:
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

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')

For conda install graphviz, I got

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 needs MS Visual C++ which I did not want to install:

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

In the end, no guide was working to really set the PATH variables correctly except for the 1st Solution approach.

所谓喜欢 2024-10-29 13:39:59

这是使用 venv python 3.8 解释器使用 pycharm 终端在 Windows 11 系统上为我工作的代码。

from graphviz import render
import os
os.environ['PATH'] = os.environ['PATH']+';' + r"C:\Program Files\Graphviz\bin" #find binaries of graphviz and add to path
render('dot','png','classes.dot')

这将创建一个classes.dot.pg文件(我不知道如何修复名称,但这是一个可以打开的png文件)

类点是在终端上生成的

pyreverse 包路径

与 pylint 一起提供。

安装:

pip 安装 pylint

(仅当您创建classes.dot 文件时才安装 pylint)

pip 安装 graphviz

Here is the code that worked for me on windows 11 system using a pycharm terminal using venv python 3.8 interpreter.

from graphviz import render
import os
os.environ['PATH'] = os.environ['PATH']+';' + r"C:\Program Files\Graphviz\bin" #find binaries of graphviz and add to path
render('dot','png','classes.dot')

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 package_path

pyreverse comes with pylint.

Installations:

pip install pylint

(install pylint only if you are creating classes.dot file)

pip install graphviz

是伱的 2024-10-29 13:39:59

这将创建一个图表“a”到“b”并将其保存为 png 文件。

代码:

from graphviz import Digraph

dot = Digraph()
dot.node('a')
dot.node('b')
dot.edge('a','b')

dot. Render("sample.png")

This would create a graph 'a' to 'b' and save it as a png file.

code:

from graphviz import Digraph

dot = Digraph()
dot.node('a')
dot.node('b')
dot.edge('a','b')

dot. Render("sample.png")
灼痛 2024-10-29 13:39:59
from graphviz import render
dot.render(directory='doctest-output', view=True)
from graphviz import render
dot.render(directory='doctest-output', view=True)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文