Adding a color attribute to nodes in NetworkX for export to Gephi

I am plotting using NetworkX for export to visualize Gephi. I add various attributes to the nodes on my graph, no problem, until I try to add colors. Does anyone know how to export a graph with color nodes using networkx? (I wrote to the gexf file, but anyway, if it's a different format, if it's compatible with Gephi.)

Here is the code in which I am plotting:

def construct_weighted_graph(nodes, distance_dict, weighting_function, color = True):

  G = nx.Graph()
  #nodes automatically added when edges added. 
  for sequence in nodes: #loop through and add size attribute for num of sequences
    G.add_node(sequence)
    G.node[sequence]['size'] = distance_dict[sequence][1] #size represented by the node
    if color:
        G.node[sequence]['color'] = (0,1,0)
  for i_node1 in range(len(nodes)):
    dist_list = distance_dict[nodes[i_node1]][-2] #list of distances
    for i_node2 in range(i_node1+1, len(nodes)):
        G.add_edge(nodes[i_node1], nodes[i_node2], 
                   weight = weighting_function(dist_list[i_node2]))
  return G

This is not exactly what I have for color, as different colors are assigned to different nodes, but this is the main idea.

+4
source share
2 answers

. NetworkX GEXF. NetworkX GEXF, , GEXF , , Gephi.

NetworkX , GEXF. node . , "viz" node. "viz" , "", , "r", "g", "b" "a".

, :

import networkx as nx
""" Create a graph with three nodes"""
graph = nx.Graph()
graph.add_node('red')
graph.add_node('green')
graph.add_node('blue')
""" Add color data """
graph.node['red']['viz'] = {'color': {'r': 255, 'g': 0, 'b': 0, 'a': 0}}
graph.node['green']['viz'] = {'color': {'r': 0, 'g': 255, 'b': 0, 'a': 0}}
graph.node['blue']['viz'] = {'color': {'r': 0, 'g': 0, 'b': 255, 'a': 0}}
""" Write to GEXF """
# Use 1.2draft so you do not get a deprecated warning in Gelphi
nx.write_gexf(graph, "file.gexf", version="1.2draft")

, node GEXF.

, :

if color:
    G.node[sequence]['viz'] = {'color': {'r': 0, 'g': 1, 'b': 0, 'a': 0}} # line changed
for i_node1 in range(len(nodes)):
+3

networkx GEXF ( 1.1draft) .

. , , ( , , : http://djotjog.com/c/wordtree/ - - ), DiGraph (G), , RGB color size <node> gexf.

...

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()

. , "viz", node "viz" dict . "" RGBA, , , . networkx, GEXF. , . stackoverflow networkx , xml.

XML- GEXF, :

<?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>

ns0:. attvalues .

. , node 'size' gexf , .

<?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>

:

http://djotjog.com/s/gexf-web2/data/ocp_network.gexf http://djotjog.com/s/gexf-web2/data/ocp_network_test.gexf

, : http://djotjog.com/s/gexf-web2?file=data/ocp_network.gexf ( ) http://djotjog.com/s/gexf-web2?file=data/ocp_network_test.gexf ( , COLOR, -.

0