How to add arbitrary node/edge attributes for export as Graphml file?
Clash 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?
graphs-and-networks
add a comment |Â
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?
graphs-and-networks
add a comment |Â
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?
graphs-and-networks
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
graphs-and-networks
asked 1 hour ago
theQman
2102
2102
add a comment |Â
add a comment |Â
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, butString
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>
add a comment |Â
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]
add a comment |Â
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, butString
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>
add a comment |Â
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, butString
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>
add a comment |Â
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, butString
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>
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, butString
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>
edited 19 mins ago
answered 1 hour ago
Szabolcs
155k13420910
155k13420910
add a comment |Â
add a comment |Â
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]
add a comment |Â
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]
add a comment |Â
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]
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]
edited 1 hour ago
answered 1 hour ago
kglr
169k8192395
169k8192395
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password