2014-06-13 24 views
0

igraph的Python界面有一個名爲metamagic的類,用於收集繪圖的圖形參數。我正在使用igraph編寫一個模塊,爲此我幾乎開始編寫自己的包裝函數,當我在文檔中找到metamagic時。但是在搜索和嘗試之後,仍然不清楚如何使用這些類。如果我定義一個AttributeCollectorBase類邊緣,就像這樣:如何使用igraph python的metamagic類?

class VisEdge(igraph.drawing.metamagic.AttributeCollectorBase): 
    width = 0.002 
    color = "#CCCCCC44" 

那麼,有沒有一種簡單的方法來傳遞這些參數給igraph.plot()功能?或者我只能一個接一個,像這樣:plot(graph,edge_color=VisEdge(graph.es).color)? 如果我想使用不是常量參數,而是通過自定義函數來計算呢?例如,vertex_size與度數成正比。 AttributeSpecification類的func參數應該這樣做,不是嗎?但我還沒有看到任何示例如何使用它。如果我定義一個AttributeSpecification情況下,像這樣:

ds = igraph.drawing.metamagic.AttributeSpecification(name="vertex_size",alt_name="size",default=2,func='degree') 

後如何將其傳遞給AtributeCollector,終於plot()

回答

2

(把事情放在上下文中:我是igraph的Python接口的作者)。我不確定metamagic包是否適合您。 AttributeCollectorBase類的唯一目的是允許igraph中的頂點和邊緣抽屜(請參閱igraph.drawing.vertexigraph.drawing.edge包)以精緻和簡潔的方式定義它們可以視爲視覺屬性的頂點和邊屬性(沒有我打字太多)。所以,舉例來說,如果你看看在igraph.drawing.vertexDefaultVertexDrawer類,你可以看到,我通過派生從AttributeCollectorBase構造VisualVertexBuilder類,如下所示:

class VisualVertexBuilder(AttributeCollectorBase): 
    """Collects some visual properties of a vertex for drawing""" 
    _kwds_prefix = "vertex_" 
    color = ("red", self.palette.get) 
    frame_color = ("black", self.palette.get) 
    frame_width = 1.0 
    ... 

後來,所使用的DefaultVertexDrawer時在DefaultGraphDrawer,我簡單構造VisualVertexBuilder如下:

vertex_builder = vertex_drawer.VisualVertexBuilder(graph.vs, kwds) 

其中graph.vs是圖表(因此頂點構建器可以訪問的頂點屬性)和kwds i的頂點序列是傳遞給plot()的一組關鍵字參數。然後,vertex_builder變量允許我通過書寫類似vertex_builder[i].color的方式檢索頂點i的計算的有效視覺屬性;在這裏,VisualVertexBuilder負責通過查看頂點並檢查它的color屬性以及查看關鍵字參數並檢查它是否包含來確定有效顏色。

底線是AttributeCollectorBase類只有在您實現自定義圖形,頂點或邊緣抽屜時纔會對您有用,並且您希望指定您希望將哪些頂點屬性視爲視覺屬性。如果您只想繪製一張圖並從其他數據中導出該特定圖的視覺屬性,那麼AttributeCollectorBase對您來說沒有用處。舉例來說,如果你想頂點的大小成比例的程度,要做到這一點是可以在本的首選方式:

sizes = rescale(graph.degree(), out_range=(0, 10)) 
plot(graph, vertex_size=sizes) 

或本:

graph.vs["size"] = rescale(graph.degree(), out_range=(0, 10)) 
plot(g) 

如果你有許多視覺上的性能,最好的方法可能是首先將它們收集到字典中,然後將該字典傳遞給plot();例如: -

visual_props = dict(
    vertex_size = rescale(graph.degree(), out_range=(0, 10)), 
    edge_width = rescale(graph.es["weight"], out_range=(0, 5), scale=log10) 
) 
plot(g, **visual_props) 

看看在rescale功能的更多詳細信息的文檔。如果您希望映射一些頂點屬性到頂點的顏色,你仍然可以使用rescale到屬性映射到0-255的範圍內,那麼他們周圍到最接近的整數,並使用一個調色板打印時:

palette = palettes["red-yellow-green"] 
colors = [round(x) for x in rescale(g.degree(), out_range=(0, len(palette)-1))] 
plot(g, vertex_color=colors, palette=palette) 
+0

所以我誤解了metamagic模塊,可能它不適合我。感謝Tamás的詳細解答,以及「rescale」功能的有用提示! – deeenes