Alas..I am on Windows. There is no python subdir in my GraphViz install
folder:
so what should I replace the dir in
sys.path.append('/usr/lib/graphviz/python/') with? Do I need to install
another python graphviz binding for Windows?
thanks
For more information:
http://code.google.com/p/python-graph/wiki/Example
Also in linux (ubuntu and debian) fail loading gv
For Ubuntu, I think that you must install the libgv-python package and
append the following directory to your
sys.path: "/usr/share/python-support/libgv-python/".
I don't know about Windows.
On Ubuntu 9.10 with python2.6, I was getting "cannot import module _gv"
when calling import gv. The fix above doesn't work. Here is what I did:
sudo mv /usr/lib/pyshared/python2.6/_gv.so
/usr/lib/pyshared/python2.6/_gv.so.old
sudo ln -s /usr/lib/pyshared/python2.6/libgv_python.so
/usr/lib/pyshared/python2.6/_gv.so
Then in python:
>>> import sys
>>> sys.path.append('/usr/lib/pyshared/python2.6')
>>> import gv
I also want to point out that the example at the top of this page is *way*
outdated. Fortunately, the pygraph module is well documented, but the
online sample can use some nice corrections.
Junk! All the gv.so should have a leading `_` (underscore), it seems the
wikimarkups stole them away. If that is the case, let me apologise to the
former poster who probably didn't notice this. Here is what you need to do,
with proper markups:
{{{
#/bin/bash
sudo mv /usr/lib/pyshared/python2.6/_gv.so
/usr/lib/pyshared/python2.6/_gv.so.old
sudo ln -s /usr/lib/pyshared/python2.6/libgv_python.so
/usr/lib/pyshared/python2.6/_gv.so
}}}
Below, is some sample code that will work, given the fix and the updates to
the pygraph modules.
{{{
#!/usr/bin/python
import sys
sys.path.append('/usr/lib/pyshared/python2.6')
import gv,pygraph
gr = pygraph.graph()
gr.add_nodes(["A","B","C","D"])
gr.add_edge("A","B")
gr.add_edge("A","C")
gr.add_edge("A","D")
gr.add_edge("B","C")
dot = pygraph.readwrite.dot.write(gr)
gvv = gv.readstring(dot)
gv.layout(gvv,"dot")
gv.render(gvv,"png","sample.png")
What is the advantage of this library over networkx? I've just recently
discovered both and wasn't sure which I should choose.
don't work at all for me
Hi,
I'm runnig Ubuntu 10.04. Here is what I did to get the first example
working. (I hope I dont' forgot any package)
sudo easy_install python-graph-core
sudo easy_install python-graph-dot
sudo apt-get install libgv-python
sudo mv /usr/lib/pyshared/python2.6/_gv.so
/usr/lib/pyshared/python2.6/_gv.so.old
sudo ln -s /usr/lib/pyshared/python2.6/libgv_python.so
/usr/lib/pyshared/python2.6/_gv.so
#!/usr/bin/python
import sys
sys.path.append('/usr/lib/pyshared/python2.6')
import gv,pygraph
gr = pygraph.graph()
gr.add_nodes(["Portugal","Spain","France","Germany","Belgium","Netherlands","Italy"])
gr.add_nodes(["Switzerland","Austria","Denmark","Poland","Czech
Republic","Slovakia","Hungary"])
gr.add_nodes(["England","Ireland","Scotland","Wales"])
gr.add_edge("Portugal", "Spain")
gr.add_edge("Spain","France")
gr.add_edge("France","Belgium")
gr.add_edge("France","Germany")
gr.add_edge("France","Italy")
gr.add_edge("Belgium","Netherlands")
gr.add_edge("Germany","Belgium")
gr.add_edge("Germany","Netherlands")
gr.add_edge("England","Wales")
gr.add_edge("England","Scotland")
gr.add_edge("Scotland","Wales")
gr.add_edge("Switzerland","Austria")
gr.add_edge("Switzerland","Germany")
gr.add_edge("Switzerland","France")
gr.add_edge("Switzerland","Italy")
gr.add_edge("Austria","Germany")
gr.add_edge("Austria","Italy")
gr.add_edge("Austria","Czech Republic")
gr.add_edge("Austria","Slovakia")
gr.add_edge("Austria","Hungary")
gr.add_edge("Denmark","Germany")
gr.add_edge("Poland","Czech Republic")
gr.add_edge("Poland","Slovakia")
gr.add_edge("Poland","Germany")
gr.add_edge("Czech Republic","Slovakia")
gr.add_edge("Czech Republic","Germany")
gr.add_edge("Slovakia","Hungary")
dot = pygraph.readwrite.dot.write(gr)
gvv = gv.readstring(dot)
gv.layout(gvv,"dot")
gv.render(gvv,"png","sample.png")
# Then, draw the breadth first search spanning tree rooted in Switzerland
st, order = pygraph.algorithms.searching.breadth_first_search(gr,
root="Switzerland")
gst = pygraph.digraph()
gst.add_spanning_tree(st)
dot = pygraph.readwrite.dot.write(gst)
gvv = gv.readstring(dot)
gv.layout(gvv,'dot')
gv.render(gvv,'png','europe-st.png')
spent two hours on it, 64 bit machine, ubuntu 10.04 - graphviz does not
work.
get this error in above sample code:
gr.add_edge(("Portugal", "Spain"))
TypeError: add_edge() takes at least 3 arguments (2 given)
sorry, have to move to networkx
About the "all cycles" question, you might want to add Johnson's algorithm
(see for instance
http://stackoverflow.com/questions/546655/finding-all-cycles-in-graph/2794683#2794683)
Hi all, I can't find if is possible save graph in xml file using
python-graph.
i.e i have got graple like below and I need save if to xml
{{{
gr = graph()
gr.add_nodes(["A","B","C","D"])
gr.add_edge(("A","D"))
gr.add_edge(("B","C"))
gr.add_edge(("A","C"))
gr.add_edge(("A","B"))
}}}
If is possible please give me example.
all the above are wrong dont waste time
Hi !
I'm working on Windows with Python 2.7.
I try your code and I have the error :
import gv
ImportError: No module named gv
I try to find the module gv for a few hours, I found graphviz but it still
doesn't work...
Can you help me please ?
So: I'm puzzled why I can't find this package's definition for the "node"
object. Is it because:
1) The documentation generator is overlooking it. Or:
2) There is no "node" class defined in this package.
If #2, then what is allowable as a node?
a) A string.
b) Strings, ints, floats.... any of Python's "scalar" values.
c) Any object.
d) Any object that would be suitable as a key in a dictionary.
e) Any object, as long as it is .... hashable? immutable? implements a
__cmp__() method?
It is quite possible I'm mixing the notion of "node" and "node
identifier". If so... please untangle me.
Thanks -- DJH
There's no node class, indeed. Any object that can be used as a dictionary
key can be used as a node. Avoid mutability in these objects (node objects
are stored internally as dictionary keys, so if they are modified they
might not be found later, corrupting the data structure).
This might be a clarity problem on the documentation. If you like, you may
file a issue requesting an improvement on these matters.
I have a doubt as below:
=====
gr = graph()
gr.add_node('1')
gr.add_node('2')
e = ('1', '2')
gr.add_edge(e, wt=3.0)
for e in gr.edges():
w = 1+gr.edge_weight(e)
gr.set_edge_weight(e,wt=round(w,3))
=====
Then the weight for edge e turns into 5 but not 4. Since it treat ('1','2')
as an edge while ('2','1') as another one. How can it fix this? Thanks!!
I have a doubt as below:
{{{
gr = graph()
gr.add_node('1')
gr.add_node('2')
e = ('1', '2')
gr.add_edge(e, wt=3.0)
for e in gr.edges():
w = 1+gr.edge_weight(e)
gr.set_edge_weight(e,wt=round(w,3))
}}}
Then the weight for edge e turns into 5 but not 4. Since it treat ('1','2')