Writing a networkx graph with position, color, ect to gexf
Question: I have constructed a graph using networkx and used the spring layout function
to generate a nice plot, unfortunately this is not transferred to the gexf
file

I think that the point of the networkx tool is to be able to write readable
graph files so I hesitate to manually change the xml if there is a simple
solution in python

here is a link to the format that I want it in because the end goal is to use
the sigma js tool to put it in a web page

<https://github.com/jacomyal/sigmajs.org/blob/master/assets/data/les-
miserables.gexf>

or more specifically this format: <http://gexf.net/format/viz.html>

this is an example from the gephi example using les miserables charsacters is
there a way that simply using the the `nx.write_gexf(G, "")` command while
also writing the properties specifically can output it in the same format?


Answer: This is more of a GEPHI "issue" rather than a Networkx one.

Very briefly,
[networkx.write_gexf](https://networkx.github.io/documentation/latest/reference/readwrite.gexf.html)
will attempt to export every possible node and edge attribute that a `gexf`
file can describe. It is then up to users of GEPHI to re-asign a particular
node or edge attribute to an internal, GEPHI attribute.

Less briefly, suppose that:

    
    
    import networkx
    
    #Create a Graph
    G = networkx.Graph()
    G.add_node("Alpha", X=10, Y=10)
    G.add_node("Beta", X=-10, Y=-10)
    G.add_path(["Alpha", "Beta"])
    

Given this graph, let's now try to save it in GEXF with:

    
    
    #Attempt to save the graph in gexf
    #PLEASE NOTE: This call will succeed and MyGraph will be created on the disk.
    #You can now do a cat MyGraph.gexf and verify that attributes X and Y are indeed included in the file.
    networkx.write_gexf(G, "MyGraph.gexf)
    
    #Add another node with an attribute of type tuple
    G.add_node("Gamma", pos=(5,5))
    
    #Attempt to save the graph in gexf again
    #PLEASE NOTE: This call will fail because it is impossible to 'unpack' the tuple without further knowledge
    networkx.write_gexf(G, "MyOtherGraph.gexf")
    

Now, a networkx.layout (e.g. `pos = networkx.layout.random_layout(G)`),
returns the positions of the nodes as an iterable array and these positions
can be saved back to the nodes but as the above example indicates, if you
attempt to save a graph with such node attributes, it will fail.

Therefore, I am afraid that you will have to unpack the coordinates returned
by a layout and assign them to single node attributes just as it is described
above (please see attributes `X` and `Y` which were used here).

Once this is done, the graph can be exported without any problems.

Now, once in Gephi and to achieve this re-assignment of a node attribute to an
internal Gephi attribute, you are first going to need to install [this
plugin](https://marketplace.gephi.org/plugin/recast-column/). Once this is
done, load your graph into GEPHI as per normal and then switch to the "Data
Laboratory" view where you can see all your nodes and their attributes.
**Provided that you have installed the recast plugin** , click on "More
actions" and then "Set Standart Column" (sic). This starts a rather self
explanatory dialog box which allows you to "map" a graph specific attribute to
an internal GEPHI attribute such as the `X-coordinate`. Use this to assign
both coordinates and then switch to the "Overview" view to see the nodes
repositioned to their saved positions.

Hope this helps.

