Python,无法使用带有anytree包的graphviz绘制树图 [英] Python, not able to graph trees using graphviz with the anytree package

查看:210
本文介绍了Python,无法使用带有anytree包的graphviz绘制树图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经安装了anytree软件包(经过很大的努力;我必须在系统上添加一些环境变量).完成此操作后,我几乎可以使用anytree包的所有功能-而不是我想要的功能.我希望将graphviz与anytree软件包结合使用,以便使用'DotExporter'命令对树进行图形绘制.

So I have installed the anytree package (after great effort; I had to add some environment variables on my system). Having done this I can use almost all functionalities of the anytree package - just not the one I want. I wish to use graphviz in conjunction with the anytree package in order to graph trees using the 'DotExporter' command.

我已经安装了graphviz;它的路径是C:\ Users \ joel \ Anaconda3_2 \ Lib \ site-packages \ graphviz,我还向我的Python文档中添加了代码行"from anytree.exporter import DotExporter"(除了需要的其他anytree导入之外)造树).

I have installed graphviz; its path is C:\Users\joel\Anaconda3_2\Lib\site-packages\graphviz, and I have also added the line of code 'from anytree.exporter import DotExporter' to my Python document (in addition to the other anytree imports needed to make trees).

因此,在定义了树的根节点"root"之后,我现在应该能够使用命令"DotExporter(root).to_picture('root.png')"生成树的png和点文件.',但是出现以下错误消息:

Therefore, having defined the root node 'root' of my tree, I should now be able to produce pngs and dot files of the tree using either of the commands 'DotExporter(root).to_picture('root.png')', however I get the following error message:

"FileNotFoundError:[WinError 2]系统找不到指定的文件".

'FileNotFoundError: [WinError 2] The system cannot find the file specified'.

有人知道发生了什么吗?我正在将Python 3.6.2和Spyder引擎一起用作Anaconda发行版的一部分(我有Anaconda 3.2).我99%肯定我拥有anytree和graphviz的最新版本.

Does anybody know what is going on? I am using Python 3.6.2 with the engine Spyder as a part of the Anaconda distribution (I have Anaconda 3.2). I am 99% certain I have the most recent versions of anytree and graphviz.

更新:因此,我了解到Anaconda和graphviz之间存在大兼容性问题已有大约一年的时间了,并认为这是问题所在.在尝试了所有解决方案后,人们建议我仍然无法使我的程序正常运行,因此我最终卸载了Anaconda,而是开始将PyCharm与标准Python解释器一起使用.但是程序 still 无法正常工作,因此,即使Anaconda和graphviz之间存在兼容性问题,事实也并不是问题所在.我在也使用Anaconda的室友的机器上尝试了该程序,并得到了与我相同的错误.

UPDATE: So I learned that there has been a big compatibility issue between Anaconda and graphviz for about a year now and assumed that this was the problem. After trying all the solutions people suggested I still wasn't able to get my program to work so I ended up uninstalling Anaconda and instead started using PyCharm together with the standard Python interpreter. However the program still wouldn't work, so even though there are compatibility issues between Anaconda and graphviz, it turns out that wasn't the problem. I tried the program on my housemate's machine which also uses Anaconda and got the same error I had been getting.

最后,我发现了以下解决方法:我无法使'DotExporter(root).to_picture('root.png')'命令正常工作,但无法使'DotExporter(root).to_dotfile('root)有效.点')'命令正在运行.".dot"文件包含代码,可以在记事本中打开(其他文字处理软件也可以使用).因此,我只需要找到一种使用点文件而不是使用'DotExporter(root).to_picture('root.png')'命令对树进行图形绘制的方法.最简单的方法是通过任一在线工具 http://www.webgraphviz.com/ http://sandbox.kidstrythisathome.com/erdos/.只需在记事本中打开点文件,复制其内容并将其粘贴到网站中,然后点击生成"即可.

In the end I found the following workaround: I couldn't get the 'DotExporter(root).to_picture('root.png')' command to work, but the 'DotExporter(root).to_dotfile('root.dot')' command was working. '.dot' files contain code and can be opened in Notepad (other word processing softwares will work too). Therefore I just had to find a way to graph the tree using the dot file instead of using the 'DotExporter(root).to_picture('root.png')' command. The easiest way to do this is via either of the online tools http://www.webgraphviz.com/ and http://sandbox.kidstrythisathome.com/erdos/. Just open your dot file in Notepad, copy its contents and paste it into the websites and hit generate.

推荐答案

这是我的解决方法.我正在使用以下初始步骤处理"入门"的示例:

Here is my workaround. I was working the examples of "getting started" with the following initial steps:

将树对象创建为 udo ,然后尝试 DotExporter(udo).to_picture("udo.png").这是我遇到与OP相同的错误的地方.

Create a tree object as udo and then trying DotExporter(udo).to_picture("udo.png"). This was where I got the same error as OP.

假设您已经安装了 graphviz python-graphviz (例如, conda install -c anaconda graphviz python-graphviz ),则可以输出图形(首先保存一个点文件):

Assuming you have already installed graphviz and python-graphviz (e.g conda install -c anaconda graphviz python-graphviz) you can output the graph (first saving a dotfile) with this:

DotExporter(udo).to_dotfile('udo.dot')

from graphviz import Source
Source.from_file('udo.dot')

然后使用以下命令保存图像文件:

Then save image file with this:

from graphviz import render
render('dot', 'png', 'udo.dot') 

这篇关于Python,无法使用带有anytree包的graphviz绘制树图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆