2016-03-21 8 views
0

python-igraphに実装されている既存のグラフィックに統計グラフィックス(プロット、軸、グラフなど)を追加するにはどうすればよいですか?私はこのライブラリの経験があるので、特にmatplotlibに興味があります。igraphでmatplotlibを使用するにはどうしたらいいですか?

私のケースでは、igraphはさまざまなレイアウトオプションに使用されています。現在のネットワークでは、レイアウトによってyが変更されている間は、x次元が部分的に制約されています。図の一番下に棒グラフを追加して、ネットワークノードのx座標に関連付けられた値の頻度をリストしたいと思います。

(私はまだとにかく、scipyのダウンロード/ ipython /パンダのライブラリを使用していない)

答えて

2

これは完全な答えではありませんが、コメントとして投稿するには長すぎるので、私のようにそれを掲示しています代わりに答え。自由に拡張/編集してください。

私はpython-igraphmatplotlibの組み合わせを試していましたが(5年以上前)、一般的な結論は2つを組み合わせることは可能ですが、かなり畳み込まれた方法でした。

python-igraphはCairoをグラフ描画バックエンドとして使用しているため(他の描画バックエンドはサポートしていないため)、描画バックエンドとしてmatplotlibを使用している場合にのみ、この組み合わせが機能します。

次に、キーのトリックは、あなたが何らかの形でmatplotlibの図形のカイロの表面を抽出して、描画対象としてIGRAPHのplot()関数にこの表面を渡すことができるということである - この場合には、IGRAPHが別々の図を作成しませんが、ちょうど指定されたサーフェスで描画を開始します。しかし、私がこれを試していた時点では、Matplotlibにはカイロ表面を図から抽出するための公開APIはありませんでした。そのため、文書化されていないMatplotlibのプロパティと関数に頼らざるを得なかったので、全体が非常に壊れやすく、 Matplotlibの特定のバージョンに大きく依存していましたが、うまくいきました。

igraph-helpメーリングリストのthis threadに全プロセスが要約されています。警告の

from matplotlib.artist import Artist 
from igraph import BoundingBox, Graph, palettes 

class GraphArtist(Artist): 
    """Matplotlib artist class that draws igraph graphs. 

    Only Cairo-based backends are supported. 
    """ 

    def __init__(self, graph, bbox, palette=None, *args, **kwds): 
     """Constructs a graph artist that draws the given graph within 
     the given bounding box. 

     `graph` must be an instance of `igraph.Graph`. 
     `bbox` must either be an instance of `igraph.drawing.BoundingBox` 
     or a 4-tuple (`left`, `top`, `width`, `height`). The tuple 
     will be passed on to the constructor of `BoundingBox`. 
     `palette` is an igraph palette that is used to transform 
     numeric color IDs to RGB values. If `None`, a default grayscale 
     palette is used from igraph. 

     All the remaining positional and keyword arguments are passed 
     on intact to `igraph.Graph.__plot__`. 
     """ 
     Artist.__init__(self) 

     if not isinstance(graph, Graph): 
      raise TypeError("expected igraph.Graph, got %r" % type(graph)) 

     self.graph = graph 
     self.palette = palette or palettes["gray"] 
     self.bbox = BoundingBox(bbox) 
     self.args = args 
     self.kwds = kwds 

    def draw(self, renderer): 
     from matplotlib.backends.backend_cairo import RendererCairo 
     if not isinstance(renderer, RendererCairo): 
      raise TypeError("graph plotting is supported only on Cairo backends") 
     self.graph.__plot__(renderer.gc.ctx, self.bbox, self.palette, *self.args, **self.kwds) 


def test(): 
    import math 

    # Make Matplotlib use a Cairo backend 
    import matplotlib 
    matplotlib.use("cairo.pdf") 
    import matplotlib.pyplot as pyplot 

    # Create the figure 
    fig = pyplot.figure() 

    # Create a basic plot 
    axes = fig.add_subplot(111) 
    xs = range(200) 
    ys = [math.sin(x/10.) for x in xs] 
    axes.plot(xs, ys) 

    # Draw the graph over the plot 
    # Two points to note here: 
    # 1) we add the graph to the axes, not to the figure. This is because 
    # the axes are always drawn on top of everything in a matplotlib 
    # figure, and we want the graph to be on top of the axes. 
    # 2) we set the z-order of the graph to infinity to ensure that it is 
    # drawn above all the curves drawn by the axes object itself. 
    graph = Graph.GRG(100, 0.2) 
    graph_artist = GraphArtist(graph, (10, 10, 150, 150), layout="kk") 
    graph_artist.set_zorder(float('inf')) 
    axes.artists.append(graph_artist) 

    # Save the figure 
    fig.savefig("test.pdf") 

    print "Plot saved to test.pdf" 

if __name__ == "__main__": 
    test() 

単語:スレッドでは、私は完全を期すためにここでそれをコピーしていますの概念実証として、次のPythonスクリプトを提供してきた私は上記のコードをテストしていません私は今私のマシンにMatplotlibを持っていないので私は今それをテストすることはできません。それはを使用して 5年前に現在のMatplotlibバージョン(0.99.3)で動作します。おそらく大きな変更なしでは今は機能しないでしょうが、それは一般的な考え方を示していて、うまく適応するのはあまり複雑ではないでしょう。

あなたのために機能するようになっている場合は、私の投稿を編集してください。

+0

サンプルコードでは、 'matplotlib.use(" cairo.pdf ")'を 'matplotlib.use(" cairo ")'に置き換える必要はありません。私がもう少し理解したら、この作業を続け、更新/承認します。 – Annan

関連する問題