How to add arbitrary node/edge attributes for export as Graphml file?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite












I would like to add arbitrary attributes to a graph G that I can access in other software, after exporting as a graphml file. If I export Export["graph.graphml", G], I simply get the information from the adjacency matrix.



Is it possible to add more attributes to the graph, like "edgecolor", "edgeweight", "nodesize", etc... that I can access after exporting?










share|improve this question

























    up vote
    5
    down vote

    favorite












    I would like to add arbitrary attributes to a graph G that I can access in other software, after exporting as a graphml file. If I export Export["graph.graphml", G], I simply get the information from the adjacency matrix.



    Is it possible to add more attributes to the graph, like "edgecolor", "edgeweight", "nodesize", etc... that I can access after exporting?










    share|improve this question























      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      I would like to add arbitrary attributes to a graph G that I can access in other software, after exporting as a graphml file. If I export Export["graph.graphml", G], I simply get the information from the adjacency matrix.



      Is it possible to add more attributes to the graph, like "edgecolor", "edgeweight", "nodesize", etc... that I can access after exporting?










      share|improve this question













      I would like to add arbitrary attributes to a graph G that I can access in other software, after exporting as a graphml file. If I export Export["graph.graphml", G], I simply get the information from the adjacency matrix.



      Is it possible to add more attributes to the graph, like "edgecolor", "edgeweight", "nodesize", etc... that I can access after exporting?







      graphs-and-networks






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      theQman

      2102




      2102




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          Mathematica does not produce standards compliant GraphML files. Many other systems will plainly refuse to read Mathematica-written GraphML. Even those that do read it, often cannot use the properties because they are in the form of Mathematica expressions converted into strings. The GraphML specification is quite clear about what data types are allowed and how they should be stored ... that's not followed.



          This is why IGraph/M includes a separate GraphML exporter. I suggest you use it.



          You do not need to do anything else than add the properties to the graph before export.



          Example:



          <<IGraphM`

          g = Graph[Property[1, "Name" -> "Alice"], Property[2, "Name" -> "Bob"],
          Property[1 <-> 2, EdgeWeight -> 123.5]];


          Here's what Export produces:



          ExportString[g, "GraphML"]


          <?xml version='1.0' encoding='UTF-8'?>
          <graphml>
          <key id='nodeKey1'
          for='node'
          attr.name='Name'
          attr.type='String' />
          <key id='nodeKey2'
          for='node'
          attr.name='VertexCoordinates'
          attr.type='String' />
          <key id='edgeKey1'
          for='edge'
          attr.name='EdgeWeight'
          attr.type='String' />
          <graph id='Graph1'
          edgedefault='undirected'>
          <node id='1'>
          <data key='nodeKey1'>Alice</data>
          <data key='nodeKey2'>List[1.`, 0.`]</data>
          </node>
          <node id='2'>
          <data key='nodeKey1'>Bob</data>
          <data key='nodeKey2'>List[-1.`, 0.`]</data>
          </node>
          <edge id='e1'
          source='1'
          target='2'>
          <data key='edgeKey1'>123.5`</data>
          </edge>
          </graph>
          </graphml>


          Major issues:



          • Does not specify the schema, so about half of the software out there does not even recognize this as GraphML.


          • Everything has attr.type='String'. string would be valid, but String is not. Some software is forgiving, but your number will still come out as strings. Moreover, they will have a ` sign appended. Even if the target software has a feature to convert string attributes into numbers, it will probably trip up on this `.


          • It exports vertex coordinates as List[...], again put in a string. No other software than Mathematica will read this, so it's rather useless.


          Here's what IGraph/M's IGExport function produces:



          IGExportString[g, "GraphML"]


          <?xml version='1.0' encoding='UTF-8'?>
          <!-- created by IGraph/M, http://szhorvat.net/mathematica/IGraphM -->
          <graphml xmlns='http://graphml.graphdrawing.org/xmlns'
          xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
          xsi:schemaLocation='http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd'>
          <key for='edge'
          id='e_EdgeWeight'
          attr.name='EdgeWeight'
          attr.type='double' />
          <key for='node'
          id='v_Name'
          attr.name='Name'
          attr.type='string' />
          <graph id='Graph'
          edgedefault='undirected'>
          <node id='1'>
          <data key='v_Name'>Alice</data>
          </node>
          <node id='2'>
          <data key='v_Name'>Bob</data>
          </node>
          <edge source='1'
          target='2'>
          <data key='e_EdgeWeight'>123.5</data>
          </edge>
          </graph>
          </graphml>


          Notice the correct XML schema and the appropriate attribute types (string and double).



          IGExport will not include Mathematica-specific properties such as VertexStyle or VertexCoordinates. If you need to export these, copy them into into custom properties with IGEdgeMap and IGVertexMap. For example,



          IGExportString[
          g // IGVertexMap[First, "x" -> GraphEmbedding] //
          IGVertexMap[Last, "y" -> GraphEmbedding],
          "GraphML"
          ]


          Snippet from the output:



           <key for='node'
          id='v_x'
          attr.name='x'
          attr.type='double' />
          <key for='node'
          id='v_y'
          attr.name='y'
          attr.type='double' />


          ...



           <node id='2'>
          <data key='v_Name'>Bob</data>
          <data key='v_x'>-1.</data>
          <data key='v_y'>0.</data>
          </node>





          share|improve this answer





























            up vote
            1
            down vote













            You can use SetProperty to define custom properties for vertices and edges:



            SeedRandom[1]
            g1 = RandomGraph[5, 9];
            nodesizes = RandomReal[1, 5];
            nodecolors = Table[Hue[RandomReal], 5];
            edgecolors = Table[Hue[RandomReal], EdgeCount[g1]];
            edgeweights = Table[Hue[RandomReal], EdgeCount[g1]];
            g1 = Fold[SetProperty[#, #2,
            "nodesize" -> nodesizes[[#2]],
            "nodecolors" -> nodecolors[[#2]]] &, g1, VertexList[g1]]
            g1 = Fold[SetProperty[#, EdgeList[g1][[#2]],
            "edgeweights" -> edgeweights[[#2]],
            "edgecolors" -> edgecolors[[#2]]] &, g1, Range@EdgeCount[g1]];

            Export[ "testg1.graphml", g1];
            Import["testg1.graphml", "EdgeAttributes", "VertexAttributes" ]



            edgecolors->Hue[0.4806592451638043], edgeweights->Hue[0.13062341735313532], edgecolors->Hue[0.8946098545557493],edgeweights->Hue[0.3209754265030236], edgecolors->Hue[0.007444617743282977],edgeweights->Hue[0.8155119957231329], edgecolors->Hue[0.741601922199022],edgeweights->Hue[0.8476492996330929], edgecolors->Hue[0.2513397456769364],edgeweights->Hue[0.33273161943614116], edgecolors->Hue[0.974394727015687],edgeweights->Hue[0.9878977012321821], edgecolors->Hue[0.8630367069779892],edgeweights->Hue[0.7838347676768282], edgecolors->Hue[0.06746415411924045],edgeweights->Hue[0.5943838055231356], edgecolors->Hue[0.5352050858030937],edgeweights->Hue[0.16349937322060293],

            nodecolors->Hue[0.1698241636720852], nodesize->0.3788643945880994, VertexCoordinates->List[0., 0.6036428791280587],

            nodecolors->Hue[0.45535872569513947], nodesize->0.9416988272914835, VertexCoordinates->List[1.006943513923487, 0.],

            nodecolors->Hue[0.7542499504147353], nodesize->0.2942640188247172, VertexCoordinates->List[0.8294432880449293, 0.6024873029730559],

            nodecolors->Hue[0.2682911877382106], nodesize->0.18827366005036095, VertexCoordinates->List[1.008260262516205, 1.2072356645553475],

            nodecolors->Hue[0.14737721617315191], nodesize->0.7615290315044017, VertexCoordinates->List[1.8707672937283246, 0.602648036371213]







            share|improve this answer






















              Your Answer





              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
              );
              );
              , "mathjax-editing");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "387"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185107%2fhow-to-add-arbitrary-node-edge-attributes-for-export-as-graphml-file%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote













              Mathematica does not produce standards compliant GraphML files. Many other systems will plainly refuse to read Mathematica-written GraphML. Even those that do read it, often cannot use the properties because they are in the form of Mathematica expressions converted into strings. The GraphML specification is quite clear about what data types are allowed and how they should be stored ... that's not followed.



              This is why IGraph/M includes a separate GraphML exporter. I suggest you use it.



              You do not need to do anything else than add the properties to the graph before export.



              Example:



              <<IGraphM`

              g = Graph[Property[1, "Name" -> "Alice"], Property[2, "Name" -> "Bob"],
              Property[1 <-> 2, EdgeWeight -> 123.5]];


              Here's what Export produces:



              ExportString[g, "GraphML"]


              <?xml version='1.0' encoding='UTF-8'?>
              <graphml>
              <key id='nodeKey1'
              for='node'
              attr.name='Name'
              attr.type='String' />
              <key id='nodeKey2'
              for='node'
              attr.name='VertexCoordinates'
              attr.type='String' />
              <key id='edgeKey1'
              for='edge'
              attr.name='EdgeWeight'
              attr.type='String' />
              <graph id='Graph1'
              edgedefault='undirected'>
              <node id='1'>
              <data key='nodeKey1'>Alice</data>
              <data key='nodeKey2'>List[1.`, 0.`]</data>
              </node>
              <node id='2'>
              <data key='nodeKey1'>Bob</data>
              <data key='nodeKey2'>List[-1.`, 0.`]</data>
              </node>
              <edge id='e1'
              source='1'
              target='2'>
              <data key='edgeKey1'>123.5`</data>
              </edge>
              </graph>
              </graphml>


              Major issues:



              • Does not specify the schema, so about half of the software out there does not even recognize this as GraphML.


              • Everything has attr.type='String'. string would be valid, but String is not. Some software is forgiving, but your number will still come out as strings. Moreover, they will have a ` sign appended. Even if the target software has a feature to convert string attributes into numbers, it will probably trip up on this `.


              • It exports vertex coordinates as List[...], again put in a string. No other software than Mathematica will read this, so it's rather useless.


              Here's what IGraph/M's IGExport function produces:



              IGExportString[g, "GraphML"]


              <?xml version='1.0' encoding='UTF-8'?>
              <!-- created by IGraph/M, http://szhorvat.net/mathematica/IGraphM -->
              <graphml xmlns='http://graphml.graphdrawing.org/xmlns'
              xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
              xsi:schemaLocation='http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd'>
              <key for='edge'
              id='e_EdgeWeight'
              attr.name='EdgeWeight'
              attr.type='double' />
              <key for='node'
              id='v_Name'
              attr.name='Name'
              attr.type='string' />
              <graph id='Graph'
              edgedefault='undirected'>
              <node id='1'>
              <data key='v_Name'>Alice</data>
              </node>
              <node id='2'>
              <data key='v_Name'>Bob</data>
              </node>
              <edge source='1'
              target='2'>
              <data key='e_EdgeWeight'>123.5</data>
              </edge>
              </graph>
              </graphml>


              Notice the correct XML schema and the appropriate attribute types (string and double).



              IGExport will not include Mathematica-specific properties such as VertexStyle or VertexCoordinates. If you need to export these, copy them into into custom properties with IGEdgeMap and IGVertexMap. For example,



              IGExportString[
              g // IGVertexMap[First, "x" -> GraphEmbedding] //
              IGVertexMap[Last, "y" -> GraphEmbedding],
              "GraphML"
              ]


              Snippet from the output:



               <key for='node'
              id='v_x'
              attr.name='x'
              attr.type='double' />
              <key for='node'
              id='v_y'
              attr.name='y'
              attr.type='double' />


              ...



               <node id='2'>
              <data key='v_Name'>Bob</data>
              <data key='v_x'>-1.</data>
              <data key='v_y'>0.</data>
              </node>





              share|improve this answer


























                up vote
                2
                down vote













                Mathematica does not produce standards compliant GraphML files. Many other systems will plainly refuse to read Mathematica-written GraphML. Even those that do read it, often cannot use the properties because they are in the form of Mathematica expressions converted into strings. The GraphML specification is quite clear about what data types are allowed and how they should be stored ... that's not followed.



                This is why IGraph/M includes a separate GraphML exporter. I suggest you use it.



                You do not need to do anything else than add the properties to the graph before export.



                Example:



                <<IGraphM`

                g = Graph[Property[1, "Name" -> "Alice"], Property[2, "Name" -> "Bob"],
                Property[1 <-> 2, EdgeWeight -> 123.5]];


                Here's what Export produces:



                ExportString[g, "GraphML"]


                <?xml version='1.0' encoding='UTF-8'?>
                <graphml>
                <key id='nodeKey1'
                for='node'
                attr.name='Name'
                attr.type='String' />
                <key id='nodeKey2'
                for='node'
                attr.name='VertexCoordinates'
                attr.type='String' />
                <key id='edgeKey1'
                for='edge'
                attr.name='EdgeWeight'
                attr.type='String' />
                <graph id='Graph1'
                edgedefault='undirected'>
                <node id='1'>
                <data key='nodeKey1'>Alice</data>
                <data key='nodeKey2'>List[1.`, 0.`]</data>
                </node>
                <node id='2'>
                <data key='nodeKey1'>Bob</data>
                <data key='nodeKey2'>List[-1.`, 0.`]</data>
                </node>
                <edge id='e1'
                source='1'
                target='2'>
                <data key='edgeKey1'>123.5`</data>
                </edge>
                </graph>
                </graphml>


                Major issues:



                • Does not specify the schema, so about half of the software out there does not even recognize this as GraphML.


                • Everything has attr.type='String'. string would be valid, but String is not. Some software is forgiving, but your number will still come out as strings. Moreover, they will have a ` sign appended. Even if the target software has a feature to convert string attributes into numbers, it will probably trip up on this `.


                • It exports vertex coordinates as List[...], again put in a string. No other software than Mathematica will read this, so it's rather useless.


                Here's what IGraph/M's IGExport function produces:



                IGExportString[g, "GraphML"]


                <?xml version='1.0' encoding='UTF-8'?>
                <!-- created by IGraph/M, http://szhorvat.net/mathematica/IGraphM -->
                <graphml xmlns='http://graphml.graphdrawing.org/xmlns'
                xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
                xsi:schemaLocation='http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd'>
                <key for='edge'
                id='e_EdgeWeight'
                attr.name='EdgeWeight'
                attr.type='double' />
                <key for='node'
                id='v_Name'
                attr.name='Name'
                attr.type='string' />
                <graph id='Graph'
                edgedefault='undirected'>
                <node id='1'>
                <data key='v_Name'>Alice</data>
                </node>
                <node id='2'>
                <data key='v_Name'>Bob</data>
                </node>
                <edge source='1'
                target='2'>
                <data key='e_EdgeWeight'>123.5</data>
                </edge>
                </graph>
                </graphml>


                Notice the correct XML schema and the appropriate attribute types (string and double).



                IGExport will not include Mathematica-specific properties such as VertexStyle or VertexCoordinates. If you need to export these, copy them into into custom properties with IGEdgeMap and IGVertexMap. For example,



                IGExportString[
                g // IGVertexMap[First, "x" -> GraphEmbedding] //
                IGVertexMap[Last, "y" -> GraphEmbedding],
                "GraphML"
                ]


                Snippet from the output:



                 <key for='node'
                id='v_x'
                attr.name='x'
                attr.type='double' />
                <key for='node'
                id='v_y'
                attr.name='y'
                attr.type='double' />


                ...



                 <node id='2'>
                <data key='v_Name'>Bob</data>
                <data key='v_x'>-1.</data>
                <data key='v_y'>0.</data>
                </node>





                share|improve this answer
























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Mathematica does not produce standards compliant GraphML files. Many other systems will plainly refuse to read Mathematica-written GraphML. Even those that do read it, often cannot use the properties because they are in the form of Mathematica expressions converted into strings. The GraphML specification is quite clear about what data types are allowed and how they should be stored ... that's not followed.



                  This is why IGraph/M includes a separate GraphML exporter. I suggest you use it.



                  You do not need to do anything else than add the properties to the graph before export.



                  Example:



                  <<IGraphM`

                  g = Graph[Property[1, "Name" -> "Alice"], Property[2, "Name" -> "Bob"],
                  Property[1 <-> 2, EdgeWeight -> 123.5]];


                  Here's what Export produces:



                  ExportString[g, "GraphML"]


                  <?xml version='1.0' encoding='UTF-8'?>
                  <graphml>
                  <key id='nodeKey1'
                  for='node'
                  attr.name='Name'
                  attr.type='String' />
                  <key id='nodeKey2'
                  for='node'
                  attr.name='VertexCoordinates'
                  attr.type='String' />
                  <key id='edgeKey1'
                  for='edge'
                  attr.name='EdgeWeight'
                  attr.type='String' />
                  <graph id='Graph1'
                  edgedefault='undirected'>
                  <node id='1'>
                  <data key='nodeKey1'>Alice</data>
                  <data key='nodeKey2'>List[1.`, 0.`]</data>
                  </node>
                  <node id='2'>
                  <data key='nodeKey1'>Bob</data>
                  <data key='nodeKey2'>List[-1.`, 0.`]</data>
                  </node>
                  <edge id='e1'
                  source='1'
                  target='2'>
                  <data key='edgeKey1'>123.5`</data>
                  </edge>
                  </graph>
                  </graphml>


                  Major issues:



                  • Does not specify the schema, so about half of the software out there does not even recognize this as GraphML.


                  • Everything has attr.type='String'. string would be valid, but String is not. Some software is forgiving, but your number will still come out as strings. Moreover, they will have a ` sign appended. Even if the target software has a feature to convert string attributes into numbers, it will probably trip up on this `.


                  • It exports vertex coordinates as List[...], again put in a string. No other software than Mathematica will read this, so it's rather useless.


                  Here's what IGraph/M's IGExport function produces:



                  IGExportString[g, "GraphML"]


                  <?xml version='1.0' encoding='UTF-8'?>
                  <!-- created by IGraph/M, http://szhorvat.net/mathematica/IGraphM -->
                  <graphml xmlns='http://graphml.graphdrawing.org/xmlns'
                  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
                  xsi:schemaLocation='http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd'>
                  <key for='edge'
                  id='e_EdgeWeight'
                  attr.name='EdgeWeight'
                  attr.type='double' />
                  <key for='node'
                  id='v_Name'
                  attr.name='Name'
                  attr.type='string' />
                  <graph id='Graph'
                  edgedefault='undirected'>
                  <node id='1'>
                  <data key='v_Name'>Alice</data>
                  </node>
                  <node id='2'>
                  <data key='v_Name'>Bob</data>
                  </node>
                  <edge source='1'
                  target='2'>
                  <data key='e_EdgeWeight'>123.5</data>
                  </edge>
                  </graph>
                  </graphml>


                  Notice the correct XML schema and the appropriate attribute types (string and double).



                  IGExport will not include Mathematica-specific properties such as VertexStyle or VertexCoordinates. If you need to export these, copy them into into custom properties with IGEdgeMap and IGVertexMap. For example,



                  IGExportString[
                  g // IGVertexMap[First, "x" -> GraphEmbedding] //
                  IGVertexMap[Last, "y" -> GraphEmbedding],
                  "GraphML"
                  ]


                  Snippet from the output:



                   <key for='node'
                  id='v_x'
                  attr.name='x'
                  attr.type='double' />
                  <key for='node'
                  id='v_y'
                  attr.name='y'
                  attr.type='double' />


                  ...



                   <node id='2'>
                  <data key='v_Name'>Bob</data>
                  <data key='v_x'>-1.</data>
                  <data key='v_y'>0.</data>
                  </node>





                  share|improve this answer














                  Mathematica does not produce standards compliant GraphML files. Many other systems will plainly refuse to read Mathematica-written GraphML. Even those that do read it, often cannot use the properties because they are in the form of Mathematica expressions converted into strings. The GraphML specification is quite clear about what data types are allowed and how they should be stored ... that's not followed.



                  This is why IGraph/M includes a separate GraphML exporter. I suggest you use it.



                  You do not need to do anything else than add the properties to the graph before export.



                  Example:



                  <<IGraphM`

                  g = Graph[Property[1, "Name" -> "Alice"], Property[2, "Name" -> "Bob"],
                  Property[1 <-> 2, EdgeWeight -> 123.5]];


                  Here's what Export produces:



                  ExportString[g, "GraphML"]


                  <?xml version='1.0' encoding='UTF-8'?>
                  <graphml>
                  <key id='nodeKey1'
                  for='node'
                  attr.name='Name'
                  attr.type='String' />
                  <key id='nodeKey2'
                  for='node'
                  attr.name='VertexCoordinates'
                  attr.type='String' />
                  <key id='edgeKey1'
                  for='edge'
                  attr.name='EdgeWeight'
                  attr.type='String' />
                  <graph id='Graph1'
                  edgedefault='undirected'>
                  <node id='1'>
                  <data key='nodeKey1'>Alice</data>
                  <data key='nodeKey2'>List[1.`, 0.`]</data>
                  </node>
                  <node id='2'>
                  <data key='nodeKey1'>Bob</data>
                  <data key='nodeKey2'>List[-1.`, 0.`]</data>
                  </node>
                  <edge id='e1'
                  source='1'
                  target='2'>
                  <data key='edgeKey1'>123.5`</data>
                  </edge>
                  </graph>
                  </graphml>


                  Major issues:



                  • Does not specify the schema, so about half of the software out there does not even recognize this as GraphML.


                  • Everything has attr.type='String'. string would be valid, but String is not. Some software is forgiving, but your number will still come out as strings. Moreover, they will have a ` sign appended. Even if the target software has a feature to convert string attributes into numbers, it will probably trip up on this `.


                  • It exports vertex coordinates as List[...], again put in a string. No other software than Mathematica will read this, so it's rather useless.


                  Here's what IGraph/M's IGExport function produces:



                  IGExportString[g, "GraphML"]


                  <?xml version='1.0' encoding='UTF-8'?>
                  <!-- created by IGraph/M, http://szhorvat.net/mathematica/IGraphM -->
                  <graphml xmlns='http://graphml.graphdrawing.org/xmlns'
                  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
                  xsi:schemaLocation='http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd'>
                  <key for='edge'
                  id='e_EdgeWeight'
                  attr.name='EdgeWeight'
                  attr.type='double' />
                  <key for='node'
                  id='v_Name'
                  attr.name='Name'
                  attr.type='string' />
                  <graph id='Graph'
                  edgedefault='undirected'>
                  <node id='1'>
                  <data key='v_Name'>Alice</data>
                  </node>
                  <node id='2'>
                  <data key='v_Name'>Bob</data>
                  </node>
                  <edge source='1'
                  target='2'>
                  <data key='e_EdgeWeight'>123.5</data>
                  </edge>
                  </graph>
                  </graphml>


                  Notice the correct XML schema and the appropriate attribute types (string and double).



                  IGExport will not include Mathematica-specific properties such as VertexStyle or VertexCoordinates. If you need to export these, copy them into into custom properties with IGEdgeMap and IGVertexMap. For example,



                  IGExportString[
                  g // IGVertexMap[First, "x" -> GraphEmbedding] //
                  IGVertexMap[Last, "y" -> GraphEmbedding],
                  "GraphML"
                  ]


                  Snippet from the output:



                   <key for='node'
                  id='v_x'
                  attr.name='x'
                  attr.type='double' />
                  <key for='node'
                  id='v_y'
                  attr.name='y'
                  attr.type='double' />


                  ...



                   <node id='2'>
                  <data key='v_Name'>Bob</data>
                  <data key='v_x'>-1.</data>
                  <data key='v_y'>0.</data>
                  </node>






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 19 mins ago

























                  answered 1 hour ago









                  Szabolcs

                  155k13420910




                  155k13420910




















                      up vote
                      1
                      down vote













                      You can use SetProperty to define custom properties for vertices and edges:



                      SeedRandom[1]
                      g1 = RandomGraph[5, 9];
                      nodesizes = RandomReal[1, 5];
                      nodecolors = Table[Hue[RandomReal], 5];
                      edgecolors = Table[Hue[RandomReal], EdgeCount[g1]];
                      edgeweights = Table[Hue[RandomReal], EdgeCount[g1]];
                      g1 = Fold[SetProperty[#, #2,
                      "nodesize" -> nodesizes[[#2]],
                      "nodecolors" -> nodecolors[[#2]]] &, g1, VertexList[g1]]
                      g1 = Fold[SetProperty[#, EdgeList[g1][[#2]],
                      "edgeweights" -> edgeweights[[#2]],
                      "edgecolors" -> edgecolors[[#2]]] &, g1, Range@EdgeCount[g1]];

                      Export[ "testg1.graphml", g1];
                      Import["testg1.graphml", "EdgeAttributes", "VertexAttributes" ]



                      edgecolors->Hue[0.4806592451638043], edgeweights->Hue[0.13062341735313532], edgecolors->Hue[0.8946098545557493],edgeweights->Hue[0.3209754265030236], edgecolors->Hue[0.007444617743282977],edgeweights->Hue[0.8155119957231329], edgecolors->Hue[0.741601922199022],edgeweights->Hue[0.8476492996330929], edgecolors->Hue[0.2513397456769364],edgeweights->Hue[0.33273161943614116], edgecolors->Hue[0.974394727015687],edgeweights->Hue[0.9878977012321821], edgecolors->Hue[0.8630367069779892],edgeweights->Hue[0.7838347676768282], edgecolors->Hue[0.06746415411924045],edgeweights->Hue[0.5943838055231356], edgecolors->Hue[0.5352050858030937],edgeweights->Hue[0.16349937322060293],

                      nodecolors->Hue[0.1698241636720852], nodesize->0.3788643945880994, VertexCoordinates->List[0., 0.6036428791280587],

                      nodecolors->Hue[0.45535872569513947], nodesize->0.9416988272914835, VertexCoordinates->List[1.006943513923487, 0.],

                      nodecolors->Hue[0.7542499504147353], nodesize->0.2942640188247172, VertexCoordinates->List[0.8294432880449293, 0.6024873029730559],

                      nodecolors->Hue[0.2682911877382106], nodesize->0.18827366005036095, VertexCoordinates->List[1.008260262516205, 1.2072356645553475],

                      nodecolors->Hue[0.14737721617315191], nodesize->0.7615290315044017, VertexCoordinates->List[1.8707672937283246, 0.602648036371213]







                      share|improve this answer


























                        up vote
                        1
                        down vote













                        You can use SetProperty to define custom properties for vertices and edges:



                        SeedRandom[1]
                        g1 = RandomGraph[5, 9];
                        nodesizes = RandomReal[1, 5];
                        nodecolors = Table[Hue[RandomReal], 5];
                        edgecolors = Table[Hue[RandomReal], EdgeCount[g1]];
                        edgeweights = Table[Hue[RandomReal], EdgeCount[g1]];
                        g1 = Fold[SetProperty[#, #2,
                        "nodesize" -> nodesizes[[#2]],
                        "nodecolors" -> nodecolors[[#2]]] &, g1, VertexList[g1]]
                        g1 = Fold[SetProperty[#, EdgeList[g1][[#2]],
                        "edgeweights" -> edgeweights[[#2]],
                        "edgecolors" -> edgecolors[[#2]]] &, g1, Range@EdgeCount[g1]];

                        Export[ "testg1.graphml", g1];
                        Import["testg1.graphml", "EdgeAttributes", "VertexAttributes" ]



                        edgecolors->Hue[0.4806592451638043], edgeweights->Hue[0.13062341735313532], edgecolors->Hue[0.8946098545557493],edgeweights->Hue[0.3209754265030236], edgecolors->Hue[0.007444617743282977],edgeweights->Hue[0.8155119957231329], edgecolors->Hue[0.741601922199022],edgeweights->Hue[0.8476492996330929], edgecolors->Hue[0.2513397456769364],edgeweights->Hue[0.33273161943614116], edgecolors->Hue[0.974394727015687],edgeweights->Hue[0.9878977012321821], edgecolors->Hue[0.8630367069779892],edgeweights->Hue[0.7838347676768282], edgecolors->Hue[0.06746415411924045],edgeweights->Hue[0.5943838055231356], edgecolors->Hue[0.5352050858030937],edgeweights->Hue[0.16349937322060293],

                        nodecolors->Hue[0.1698241636720852], nodesize->0.3788643945880994, VertexCoordinates->List[0., 0.6036428791280587],

                        nodecolors->Hue[0.45535872569513947], nodesize->0.9416988272914835, VertexCoordinates->List[1.006943513923487, 0.],

                        nodecolors->Hue[0.7542499504147353], nodesize->0.2942640188247172, VertexCoordinates->List[0.8294432880449293, 0.6024873029730559],

                        nodecolors->Hue[0.2682911877382106], nodesize->0.18827366005036095, VertexCoordinates->List[1.008260262516205, 1.2072356645553475],

                        nodecolors->Hue[0.14737721617315191], nodesize->0.7615290315044017, VertexCoordinates->List[1.8707672937283246, 0.602648036371213]







                        share|improve this answer
























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          You can use SetProperty to define custom properties for vertices and edges:



                          SeedRandom[1]
                          g1 = RandomGraph[5, 9];
                          nodesizes = RandomReal[1, 5];
                          nodecolors = Table[Hue[RandomReal], 5];
                          edgecolors = Table[Hue[RandomReal], EdgeCount[g1]];
                          edgeweights = Table[Hue[RandomReal], EdgeCount[g1]];
                          g1 = Fold[SetProperty[#, #2,
                          "nodesize" -> nodesizes[[#2]],
                          "nodecolors" -> nodecolors[[#2]]] &, g1, VertexList[g1]]
                          g1 = Fold[SetProperty[#, EdgeList[g1][[#2]],
                          "edgeweights" -> edgeweights[[#2]],
                          "edgecolors" -> edgecolors[[#2]]] &, g1, Range@EdgeCount[g1]];

                          Export[ "testg1.graphml", g1];
                          Import["testg1.graphml", "EdgeAttributes", "VertexAttributes" ]



                          edgecolors->Hue[0.4806592451638043], edgeweights->Hue[0.13062341735313532], edgecolors->Hue[0.8946098545557493],edgeweights->Hue[0.3209754265030236], edgecolors->Hue[0.007444617743282977],edgeweights->Hue[0.8155119957231329], edgecolors->Hue[0.741601922199022],edgeweights->Hue[0.8476492996330929], edgecolors->Hue[0.2513397456769364],edgeweights->Hue[0.33273161943614116], edgecolors->Hue[0.974394727015687],edgeweights->Hue[0.9878977012321821], edgecolors->Hue[0.8630367069779892],edgeweights->Hue[0.7838347676768282], edgecolors->Hue[0.06746415411924045],edgeweights->Hue[0.5943838055231356], edgecolors->Hue[0.5352050858030937],edgeweights->Hue[0.16349937322060293],

                          nodecolors->Hue[0.1698241636720852], nodesize->0.3788643945880994, VertexCoordinates->List[0., 0.6036428791280587],

                          nodecolors->Hue[0.45535872569513947], nodesize->0.9416988272914835, VertexCoordinates->List[1.006943513923487, 0.],

                          nodecolors->Hue[0.7542499504147353], nodesize->0.2942640188247172, VertexCoordinates->List[0.8294432880449293, 0.6024873029730559],

                          nodecolors->Hue[0.2682911877382106], nodesize->0.18827366005036095, VertexCoordinates->List[1.008260262516205, 1.2072356645553475],

                          nodecolors->Hue[0.14737721617315191], nodesize->0.7615290315044017, VertexCoordinates->List[1.8707672937283246, 0.602648036371213]







                          share|improve this answer














                          You can use SetProperty to define custom properties for vertices and edges:



                          SeedRandom[1]
                          g1 = RandomGraph[5, 9];
                          nodesizes = RandomReal[1, 5];
                          nodecolors = Table[Hue[RandomReal], 5];
                          edgecolors = Table[Hue[RandomReal], EdgeCount[g1]];
                          edgeweights = Table[Hue[RandomReal], EdgeCount[g1]];
                          g1 = Fold[SetProperty[#, #2,
                          "nodesize" -> nodesizes[[#2]],
                          "nodecolors" -> nodecolors[[#2]]] &, g1, VertexList[g1]]
                          g1 = Fold[SetProperty[#, EdgeList[g1][[#2]],
                          "edgeweights" -> edgeweights[[#2]],
                          "edgecolors" -> edgecolors[[#2]]] &, g1, Range@EdgeCount[g1]];

                          Export[ "testg1.graphml", g1];
                          Import["testg1.graphml", "EdgeAttributes", "VertexAttributes" ]



                          edgecolors->Hue[0.4806592451638043], edgeweights->Hue[0.13062341735313532], edgecolors->Hue[0.8946098545557493],edgeweights->Hue[0.3209754265030236], edgecolors->Hue[0.007444617743282977],edgeweights->Hue[0.8155119957231329], edgecolors->Hue[0.741601922199022],edgeweights->Hue[0.8476492996330929], edgecolors->Hue[0.2513397456769364],edgeweights->Hue[0.33273161943614116], edgecolors->Hue[0.974394727015687],edgeweights->Hue[0.9878977012321821], edgecolors->Hue[0.8630367069779892],edgeweights->Hue[0.7838347676768282], edgecolors->Hue[0.06746415411924045],edgeweights->Hue[0.5943838055231356], edgecolors->Hue[0.5352050858030937],edgeweights->Hue[0.16349937322060293],

                          nodecolors->Hue[0.1698241636720852], nodesize->0.3788643945880994, VertexCoordinates->List[0., 0.6036428791280587],

                          nodecolors->Hue[0.45535872569513947], nodesize->0.9416988272914835, VertexCoordinates->List[1.006943513923487, 0.],

                          nodecolors->Hue[0.7542499504147353], nodesize->0.2942640188247172, VertexCoordinates->List[0.8294432880449293, 0.6024873029730559],

                          nodecolors->Hue[0.2682911877382106], nodesize->0.18827366005036095, VertexCoordinates->List[1.008260262516205, 1.2072356645553475],

                          nodecolors->Hue[0.14737721617315191], nodesize->0.7615290315044017, VertexCoordinates->List[1.8707672937283246, 0.602648036371213]








                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 1 hour ago

























                          answered 1 hour ago









                          kglr

                          169k8192395




                          169k8192395



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185107%2fhow-to-add-arbitrary-node-edge-attributes-for-export-as-graphml-file%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              Long meetings (6-7 hours a day): Being “babysat” by supervisor

                              Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                              Confectionery