2017-01-18 7 views
0

ノードクラスにドットが付いている隣接行列をプロットします。python - ノードクラスに応じて色付きノードをプロットする隣接行列

xとyのノード間のエッジが存在する場合:thenodesの両方がクラス1 =>カラーブルー に属している場合、ノードの両方が赤クラス0 =>カラー

  • に属している場合

    • ノードの両方がクラス2 =>色緑色
    • 他=>カラーグレー

    に属している場合、私は隣接マットを有します(NXなど)networkx からRIXは言うことができます:

    matrix = np.array([[1 0 0 0 0],[1 0 1 0 0],[1 1 0 1 0],[1 0 0 0 1],[1 0 1 0 0]]) 
    

    私も0または1または2

    • ノード0にそれをclassifing各ノードのための 'network_num' と呼ばれる属性がある - > 0を
    • ノード1 - > 0
    • ノード2 - > 1
    • ノード3 - > 1
    • ノード4 - > 2

      network_num =私はそれを前提とするつもりです

  • +0

    はあなたが "隣接行列をプロットする" の意味を明確することはできますか?あなたはそれらの色を持つノードでグラフを描きたいですか? http:// stackoverflow。com/questions/24662006/python-networkx-graph-different-colored-nodes-using-two-listsがあなたの質問に答えるかもしれません。 – Joel

    答えて

    0

    {0:0,1:0,2:1,3:1,4 2}、あなたが意味する "隣接行列をプロット"ちょうどそれが貧弱な行列をプロットし、「点」によって行列の要素を意味するということです。コードで

    は、以下の私が作成するためにあなたの matrixnetwork_numを使用し、私の(plotedされます) drawn_matrix。 エッジが存在することを確認したら、「赤」の場合は drawn_matrix、青の場合は0.5、「緑」の場合は1.0を割り当てます。 私は灰色としてエッジがない( matrixの0)とマークしました。

    import numpy as np 
    import matplotlib.pyplot as plt 
    import matplotlib.colors as mcolors 
    
    # Graph data 
    network_num = {0: 0, 1: 0, 2: 1, 3: 1, 4: 2} 
    matrix = np.array([[1, 0, 0, 0, 0], 
            [1, 0, 1, 0, 0], 
            [1, 1, 0, 1, 0], 
            [1, 0, 0, 0, 1], 
            [1, 0, 1, 0, 0]]) 
    
    drawn_matrix = np.zeros((5, 5)) 
    
    # Iterate 5x5 matrix 
    for row in range(5): 
        for col in range(5): 
         # check if edge exists 
         if matrix[row][col] == 1: 
          # red 
          if network_num[row] == 0 and network_num[col] == 0: 
           drawn_matrix[row][col] = 0.25 
          # blue 
          elif network_num[row] == 1 and network_num[col] == 1: 
           drawn_matrix[row][col] = 0.5 
          # green 
          elif network_num[row] == 2 and network_num[col] == 2: 
           drawn_matrix[row][col] = 1.0 
          # gray 
          else: 
           drawn_matrix[row][col] = 0.0 
         else: 
          drawn_matrix[row][col] = 0.0 # no edge is marked as gray 
    
    print("Matrix with color info:") 
    print(drawn_matrix) 
    

    ここに出力(私がプロットするマトリックス)があります。 matrixnetwork_numに基づいて、プロットに緑色は表示されません。

    Matrix with color info: 
    [[ 0.25 0. 0. 0. 0. ] 
    [ 0.25 0. 0. 0. 0. ] 
    [ 0. 0. 0. 0.5 0. ] 
    [ 0. 0. 0. 0. 0. ] 
    [ 0. 0. 0. 0. 0. ]] 
    

    ここでは正確なプロットです。ここで私はthis anwser非常に有用見つける。 コードでは、次の色のカスタムカラーマップを定義します:灰色、赤色、青色、緑色。 rvbの数値は、前に投稿したコードの値と一致します。

    def make_colormap(seq): 
        """Return a LinearSegmentedColormap 
        seq: a sequence of floats and RGB-tuples. The floats should be increasing 
        and in the interval (0,1). 
        """ 
        seq = [(None,) * 3, 0.0] + list(seq) + [1.0, (None,) * 3] 
        cdict = {'red': [], 'green': [], 'blue': []} 
        for i, item in enumerate(seq): 
         if isinstance(item, float): 
          r1, g1, b1 = seq[i - 1] 
          r2, g2, b2 = seq[i + 1] 
          cdict['red'].append([item, r1, r2]) 
          cdict['green'].append([item, g1, g2]) 
          cdict['blue'].append([item, b1, b2]) 
        return mcolors.LinearSegmentedColormap('CustomMap', cdict) 
    
    c = mcolors.ColorConverter().to_rgb 
    rvb = make_colormap(
        [c('gray'), c('red'), 0.25, c('red'), c('blue'), 0.5, c('blue'), c('green'), 0.75, c('green')]) 
    
    plt.matshow(drawn_matrix, vmin=0.0, vmax=1.0, cmap=rvb) 
    plt.show() 
    

    最終隣接行列:

    enter image description here

    関連する問題