networkx
conversion to GEXF format (version 1.1draft) is buggy for me too.
But this worked for me. I'm adding some extra code before and after the part you need, because this implies (in my cursory rereading of code that I wrote years ago but continue to use on this site: http://djotjog.com/c/wordtree/ -- because it still works) that you add the nodes to the DiGraph (G) then have to add them again in a different way ensure the RGB color
and size
format is coded inside the <node>
tags of the gexf file.
code...
G.add_nodes_from(nodes) #<--- assigns node names, and also node 'size', used below
for n in G.node:
n_size = G.node[n]['size']
G.add_node(n,viz={'color':{'r':"170",'b':"170",'g':"170",'a':"0.7"},'size':n_size})
#{'position':{'x':x,'y':y}}
G.add_weighted_edges_from(edges)
...
import codecs
f = codecs.open(gexf_filename, "wb", "utf-8") # unicode safe version of file-open
nx.write_gexf(G,f)
f.close()
Note: You could add more than just size and color to the 'viz' part, but every node must have a 'viz' dict for color to work. I also assigned alpha "a" here as part of RGBA format, but it doesn't seem to be appearing in my graphs. networkx strips that part out of GEXF. I think that is a bug. Elsewhere on stackoverflow I've discovered another networkx bug in not being able to control the namespace in the xml header.
That code yields GEXF XML nodes that look like this:
<?xml version="1.0" encoding="utf-8"?><gexf xmlns:ns0="http://www.gexf.net/1.1draft/viz" version="1.1" xmlns="http://www.gexf.net/1.1draft" xmlns:viz="http://www.gexf.net/1.1draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
<graph defaultedgetype="directed" mode="static">
<attributes class="node" mode="static">
<attribute id="0" title="size" type="integer" />
</attributes>
<nodes>
<node id="0" label="shop">
<ns0:color b="100" g="100" r="100" />
<ns0:size value="17" />
<attvalues>
<attvalue for="0" value="17" />
</attvalues>
</node>
Where the ns0:
part is crucial. Putting color in attvalues
doesn't translate into color in the graph.
This version has no color. And note that node 'size' MUST be defined in gexf format to work, and it must be an integer.
<?xml version='1.0' encoding='utf-8'?>
<gexf version="1.1" xmlns="http://www.gexf.net/1.1draft" xmlns:viz="http://www.gexf.net/1.1draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
<attributes class="node" mode="static">
<attribute id="0" title="category" type="string" />
<attribute id="1" title="color" type="string" />
<attribute id="2" title="size" type="integer" />
</attributes>
<node id="CarterCenter" label="CarterCenter">
<attvalues>
<attvalue for="0" value="open data" />
<attvalue for="1" value="blue" />
<attvalue for="2" value="0" />
</attvalues>
</node>
Here are two useful examples of files for debugging purposes:
http://djotjog.com/s/gexf-web2/data/ocp_network.gexf
versus
http://djotjog.com/s/gexf-web2/data/ocp_network_test.gexf
and you can see them render online here:
http://djotjog.com/s/gexf-web2?file=data/ocp_network.gexf (WITH COLOR)
and
http://djotjog.com/s/gexf-web2?file=data/ocp_network_test.gexf (SAME CHART, without COLOR, but with alpha working.