2016-01-07 51 views
0

我正在嘗試使用networkx製作一個動態圖表。例如,當我使用:在python的Networkx中製作動態圖表

import networkx as nx 
G = nx.Graph() 

它使一個空的圖G,它默認是靜態的。我怎樣才能將其改變爲動態?一些節點/邊可能存在於一個時間戳中,但其他節點/邊可能不存在。我如何合併時間? 舉個例子,假設我的圖有三個節點'a','b'和'c'。在t1時刻,'a'和'b'之間只有一條邊。在時間t2,邊緣的配置改變,並且在該時間戳中,所有節點彼此連接。說:

G.add_edge('a','b',timestamp='t1') 
G.add_node('c',timestamp='t1') 

G.add_edge('a','b',timestamp='t2') 
G.add_edge('a','c',timestamp='t2') 
G.add_edge('b','c',timestamp='t2') 

但這是行不通的!

我需要這樣做的原因是我想將gexf格式的圖保存在Gephi中。

我該怎麼辦?

+0

那麼你是否每次都有完全不同的圖,或者只有一些邊和某些節點發生了變化? – Joel

+0

謝謝你糾正我,喬爾。是的,應該是「節點」!這是一個隨時間發展的圖表。它可能獲得/失去節點或邊緣。 –

回答

3

使用nx.Graph()在2個節點之間不能有多個邊。 但是,如果您使用nx.MultiGraph(),您可以。

你可以做的是用一個簡單的計數器將圖的節點索引爲節點ID和時間戳作爲屬性。如果要使用nx.get_node_attributes(G, 'id')選擇子集,則必須根據其屬性過濾節點。

通常情況下,NetworkX不適用於動態圖形。您可以使用專爲此設計的Stinger

編輯:對於格法,它是完全不同的。你應該用GEXF導出圖表並祈求Gephi閱讀它。基本上是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2" xmlns:viz="http://www.gexf.net/1.2draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd"> 
    <meta lastmodifieddate="2016-01-07"> 
    <creator>Gephi 0.8.1</creator> 
    <description></description> 
    </meta> 
    <graph defaultedgetype="directed" timeformat="double" mode="dynamic"> 
    <attributes class="node" mode="dynamic"> 
     <attribute id="score" title="score" type="integer"></attribute> 
    </attributes> 
    <attributes class="edge" mode="dynamic"> 
     <attribute id="weight" title="Weight" type="float"></attribute> 
    </attributes> 
    <nodes> 
     <node id="n0" label="n0" start="2001.0" end="2022.0"> 
     <attvalues> 
      <attvalue for="score" value="4" start="2001.0" end="2010.0"></attvalue> 
      <attvalue for="score" value="3" start="2011.0" end="2011.0"></attvalue> 
     </attvalues> 
     <viz:size value="10.0"></viz:size> 
     <viz:position x="46.152466" y="-287.68558" z="0.0"></viz:position> 
     <viz:color r="25" g="213" b="100"></viz:color> 
     </node> 
     <node id="n1" label="n1" start="2001.0" end="2024.0"> 
     <attvalues> 
      <attvalue for="score" value="2" start="2001.0" end="2007.0"></attvalue> 
      <attvalue for="score" value="3" start="2008.0" end="2008.0"></attvalue> 
     </attvalues> 
     <viz:size value="10.0"></viz:size> 
     <viz:position x="-293.90674" y="-442.9504" z="0.0"></viz:position> 
     <viz:color r="25" g="213" b="100"></viz:color> 
     </node> 
    </nodes> 
    <edges> 
     <edge source="n0" target="n1"> 
     <attvalues> 
      <attvalue for="weight" value="3.0" start="2010.0" endopen="2012.0"></attvalue> 
      <attvalue for="weight" value="4.0" start="2012.0" endopen="2014.0"></attvalue> 
      <attvalue for="weight" value="4.0" start="2014.0" endopen="2016.0"></attvalue> 
      <attvalue for="weight" value="4.0" start="2016.0" endopen="2018.0"></attvalue> 
      <attvalue for="weight" value="5.0" start="2018.0" endopen="2020.0"></attvalue> 
      <attvalue for="weight" value="6.0" start="2020.0" endopen="2022.0"></attvalue> 
      <attvalue for="weight" value="8.0" start="2022.0" endopen="2024.0"></attvalue> 
      <attvalue for="weight" value="9.0" start="2024.0" end="2026.0"></attvalue> 
     </attvalues> 
     </edge> 
    </edges> 
    </graph> 
</gexf> 

我不確定你可以直接用Networkx來實現。 People have tried。重要的是,邊緣應該具有startend屬性兩倍。節點相同。那麼每條邊都應該有一個三元組列表:

[{'start': s, 'end' or 'endopen': e, 'weight': 4}, {..}] 
+0

謝謝。但是,我需要這樣做的原因是我需要將圖形保存爲gexf格式以在Gephi中使用它。如果我使用屬性來表示時間戳並將其保存爲gexf,那麼Gephi不會將其識別爲「動態」圖。 –

+0

你的問題沒有提到格西。我會修改答案。 – Kikohs

+0

是的,我曾嘗試過。但我不能做任何像 timeformat =「double」mode =「dynamic」 在一個圖表由網絡x。 我也嘗試導入一個Gephi的動態gexf到python中,所以我可能會想到它,但是networkx不能識別它! –