Commit 95484cb5 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Some improvements in graph_draw()

Vertex/edge colors are properly plotted even when value range is zero
lenght. The behaviour of the 'fork' option was slightly modified.
parent 18445c39
...@@ -34,7 +34,7 @@ def graph_draw(g, pos=None, size=(15,15), pin=False, layout="neato", ...@@ -34,7 +34,7 @@ def graph_draw(g, pos=None, size=(15,15), pin=False, layout="neato",
gprops={}, vprops={}, eprops={}, vcolor=None, ecolor=None, gprops={}, vprops={}, eprops={}, vcolor=None, ecolor=None,
vcmap=matplotlib.cm.jet, vnorm=True, ecmap=matplotlib.cm.jet, vcmap=matplotlib.cm.jet, vnorm=True, ecmap=matplotlib.cm.jet,
enorm=True, output="", output_format="auto", returngv=False, enorm=True, output="", output_format="auto", returngv=False,
fork=True, seed=0): fork=False, seed=0):
"""Draw a graph using graphviz.""" """Draw a graph using graphviz."""
if output != "": if output != "":
...@@ -88,6 +88,8 @@ def graph_draw(g, pos=None, size=(15,15), pin=False, layout="neato", ...@@ -88,6 +88,8 @@ def graph_draw(g, pos=None, size=(15,15), pin=False, layout="neato",
c = vcolor[v] c = vcolor[v]
minmax[0] = min(c,minmax[0]) minmax[0] = min(c,minmax[0])
minmax[1] = max(c,minmax[1]) minmax[1] = max(c,minmax[1])
if minmax[0] == minmax[1]:
minmax[1] += 1
if vnorm: if vnorm:
vnorm = matplotlib.colors.normalize(vmin=minmax[0], vmax=minmax[1]) vnorm = matplotlib.colors.normalize(vmin=minmax[0], vmax=minmax[1])
...@@ -97,6 +99,8 @@ def graph_draw(g, pos=None, size=(15,15), pin=False, layout="neato", ...@@ -97,6 +99,8 @@ def graph_draw(g, pos=None, size=(15,15), pin=False, layout="neato",
c = ecolor[e] c = ecolor[e]
minmax[0] = min(c,minmax[0]) minmax[0] = min(c,minmax[0])
minmax[1] = max(c,minmax[1]) minmax[1] = max(c,minmax[1])
if minmax[0] == minmax[1]:
minmax[1] += 1
if enorm: if enorm:
enorm = matplotlib.colors.normalize(vmin=minmax[0], vmax=minmax[1]) enorm = matplotlib.colors.normalize(vmin=minmax[0], vmax=minmax[1])
...@@ -211,12 +215,12 @@ def graph_draw(g, pos=None, size=(15,15), pin=False, layout="neato", ...@@ -211,12 +215,12 @@ def graph_draw(g, pos=None, size=(15,15), pin=False, layout="neato",
# if using xlib we need to fork the process, otherwise good ol' graphviz # if using xlib we need to fork the process, otherwise good ol' graphviz
# will call exit() when the window is closed # will call exit() when the window is closed
if output_format == "xlib": if output_format == "xlib" or fork:
pid = os.fork() pid = os.fork()
if pid == 0: if pid == 0:
gv.render(gvg, output_format, output) gv.render(gvg, output_format, output)
sys.exit(1) # since we forked, it's good to be sure os._exit(0) # since we forked, it's good to be sure
if not fork: if output_format != "xlib":
os.wait() os.wait()
else: else:
gv.render(gvg, output_format, output) gv.render(gvg, output_format, output)
...@@ -225,4 +229,5 @@ def graph_draw(g, pos=None, size=(15,15), pin=False, layout="neato", ...@@ -225,4 +229,5 @@ def graph_draw(g, pos=None, size=(15,15), pin=False, layout="neato",
return pos, gv return pos, gv
else: else:
gv.rm(gvg) gv.rm(gvg)
del gvg
return pos return pos
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment