2016-10-18 6 views
2

私はグラフィックを視覚化したい101x101のマトリックスを持っています。これまでのところ、私は以下のようにmatplotlib.pyplotから機能matshowを使用:matshow()の代替

import numpy as np 
import random 
import matplotlib.pyplot as plt 

A = np.zeros((101,101)) 
# do stuff to randomly change some values of A 
plt.ion() 
plt.matshow(A,cmap='PuBuGn') 
plt.colorbar() 
plt.show() 

出力は次のようになります。 enter image description here

あなたは種間の相互作用行列としてこれを見て、あなただけがある見ることができるようにすべきです3つの種が強く相互作用している。それは私がpaperで見つかった以下のグラフのような可視化を検討することが適切に見える理由ですが、どのように、これはPythonで実装することができるかどうかはわからない:

enter image description here

答えて

3

これはトリックを行う必要があります。

import numpy as np 
import matplotlib.pyplot as plt 

def old_graph(A): 
    plt.matshow(A,cmap='PuBuGn') 
    plt.colorbar() 
    plt.title(r"abs$\left(\left[\mathbf{A}_{ij} \right ] \right)$ ; SIS=%d"%(sis,), va='bottom') 
    plt.show() 

def new_graph(A, sis_list=np.zeros(0,int), symmetric=True, fig=None, pos=111): 
    #create and edit figure: 
    if fig is None: 
     fig = plt.figure() 
    ax = fig.add_subplot(pos, projection='polar') 
    ax.set_rgrids([1],[' ']) 
    ax.set_rmax(1) 
    ax.set_thetagrids([]) 
    ax.set_title(r"abs$\left(\left[\mathbf{A}_{ij} \right ] \right)$ ; SIS=%d"%(sis,), va='bottom') 
    colormap = plt.get_cmap('PuBuGn') 

    # make each species an angle value: 
    n_species = A.shape[0] 
    angles = np.linspace(0, 2*np.pi, n_species+1) 
    # the radius will always be r_max, and each line 
    # will always unite two points: 
    r = np.ones((2,)) 
    # prepare list of lines to sort: 
    unordered_pairs_and_values = [] 
    for index_line in xrange(n_species): 
     for index_column in xrange(index_line): 
      if symmetric: 
       value = A[index_line,index_column] 
      else: # not symmetric 
       value= abs(A[index_line,index_column]- A[index_column,index_line]) 
      unordered_pairs_and_values.append([[angles[index_line],angles[index_column]], value]) 
    # sort the lines (otherwise white lines would cover the 'important' ones): 
    ordered_pairs_and_values = sorted(unordered_pairs_and_values, key=lambda pair: pair[1]) 
    # get the maximum value for scaling: 
    I_max = ordered_pairs_and_values[-1][1] 
    # plot every line in order: 
    for pair in ordered_pairs_and_values: 
     ax.plot(pair[0], r, color=colormap(pair[1]/I_max), linewidth=2, alpha=0.8) 
    # don't know how to add the colorbar: 
    #fig.colorbar(orientation='horizontal') 
    # mark the angles: 
    ax.plot(angles, np.ones(angles.shape), 'ko') 
    # mark the important angles (comment if you don't know which ones are these): 
    ax.plot(angles[sis_list], np.ones(sis_list.shape), 'ro') 
    fig.show() 

if __name__ == '__main__': 
    n_species = 51 
    sis = 3 # strongly interacting species 
    sis_factor = 4. 
    A = np.zeros((n_species,n_species)) 
    # do stuff to randomly change some values of A: 
    for index_line in xrange(n_species): 
     for index_column in xrange(index_line+1): 
      A[index_line,index_column] = np.random.random() 
      A[index_column,index_line] = A[index_line,index_column] 

    sis_list = np.random.randint(0,n_species,sis) 
    for species in sis_list: 
     A[species,:] *= sis_factor 
     A[:,species] *= sis_factor 
     for species2 in sis_list: # correct crossings 
      A[species,species2] /= sis_factor 
    # stuff to randomly change some values of A done 
    old_graph(A=A) 
    new_graph(A=A, sis_list=sis_list, symmetric=True) 

旧グラフ: old_graph

新しいグラフ: enter image description here I S

  • カラーマップの入力方法は、カラーマップを使用してマッピングされたプロットが必要なためです。グラフを再スケーリングすることなく、R-ダニを除去するための方法を
  • (あなたが試すことができますコメントを外し#ax.set_rticks([])

、プロットは、以下の相互作用種と良く見えます: enter image description here

+1

私は忘れる前に、私は半分しか考えられ1つの種が他の種と相互作用したとき、その相互作用が反対方向で等価であると仮定したので(したがって、A [i、j]はA [j、i]と等しくなければならない)さもなければ、私は2つの種の間の線に色を付けるためにどの値を使うのか分からないでしょう。 – berna1111

+0

これはすでに私に多くの助けになります。行列Aは[論文](http://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1004688)では対称ではありませんが、著者はその半分をそうでなければ、あなたが正しく述べたようなダイアグラムを構築する方法はありません。 –

+1

wolfram alphaの[this](https://reference.wolfram.com/language/ref/WeightedAdjacencyMatrix.html)と[this](http://www.mathworks.com/help/matlab/ref/)が見つかりました。 graph.adjacency.html?requestedDomain = www.mathworks.com)Matlabのために、あなたが望むものかもしれません。最後に、あなたの作品はネットワークといくつかの並列性を持っているようですが、[この答えは見つかりませんでした](http://stackoverflow.com/questions/33773187/efficiently-create-adjacency-matrix-from-network-graph-vice-versa-python -netwo)。 – berna1111