Issue
I am attempting to use Graphviz from Spyder (via an Anaconda install). I am having trouble understanding what is needed to do this and how to go about loading packages, setting variables, etc.
I straight forward approach for a new Python and Graphviz and Spyder user would be great!
Also, apart from just creating and running Graphviz, how can one run Graphviz from python with a pre-generated .gv file?
Solution
Here are the steps that worked for me. Much of this information was available but spread out in several different StackOverflow posts and other websites. I hope that this serves as a good one-stop resource.
Go to the Graphviz website and download and install to your computer (do NOT need to install for all users).
Download and install Anaconda3.5 from the Continuum website.
Add Graphviz to the environment variable "Path":
- Go to
Computer > Properties > Advanced system settings > Environment Variables
and then find "Path" in the system variables box. Click on Path and click edit. - Append
;C:\Program Files (x86)\Graphviz2.38\bin
to the end of the many paths that are already present in Path. Note, the path to Graphviz may be different for you so make sure to put the correct path. The folder "bin" should have many files including thedot.exe
application. - To check the install go to the command prompt and enter:
dot -V
this should return the version of Graphviz installed. For example,dot - graphviz version 2.38.0
. If this does not work, enterset
and look for the Graphviz path.
- Go to
Go to the Anaconda command prompt and enter:
pip install graphviz
Restart Spyder or launch it if not already open.
Now within your Python script add
import graphviz
Below is an example of how to create a graph and render it using Graphviz from a Graphviz tutorial
import graphviz dot = graphviz.Digraph(comment='The Round Table') dot.node('A', 'King Arthur') dot.node('B', 'Sir Bedevere the Wise') dot.node('L', 'Sir Lancelot the Brave') dot.edges(['AB', 'AL']) dot.edge('B', 'L', constraint='false') dot.render('FileName', view=True)
Below is an example of how to create a graph from a pre-generated .gv file (at least a starting point for exploration)
from graphviz import Source Source.from_file('file.gv')
Usefull Links:
Getting started with Graphviz and Python
Another StackOverflow Question
Versions Used:
Anaconda 3.5 (comes with Spyder)
Graphviz 2.38
Answered By - Scott G
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.