2016-01-09 35 views
-1

我想繪製隔離年份差異和核苷酸差異發生的病毒生物序列組合的頻率。我試圖找到一個優雅的方式來做到這一點有麻煩。計算x,y座標的頻率,以二維方式顯示並繪圖

所以我有一個對齊方式,我比較每個序列彼此以得到他們是如何不同的整數值。我也會檢查一下他們隔離的年份有多不同。因此,對於一組相隔兩年並且有三個差異的序列,您可以獲得座標(2,3)。我想要統計出現多少次(2,3)以及所有其他組合並繪製它(並獲取繪圖數據)。我一直在嘗試將頻率列表轉換爲數據幀,但我不知道是否有更好的方法來做到這一點。

我可以顯示一些代碼,但我不確定這是最好的方式,所以我想聽聽其他的想法。

一個問題是如何在開始時表示頻率。我可以創建一個所有事件的列表,或創建一個事件字典並增加一個計數器。

樣本數據: (年差,序列殘基差異): (1,2),(2,5),(1,2),(5,5),(4,5)

輸出顯示在圖片中,但不一定是表格結構。 CSV是首選。 output

+0

您可以發佈您的數據樣本,什麼解決方案應該是什麼樣子 – Chris

+0

可能的複製[自定義matplotlib陰謀:國際象棋棋盤如表彩色細胞( HTTP://計算器。com/questions/10194482/custom-matplotlib-plot-chess-board-like-table-with-colored-cells) – Reti43

+0

Reti43確實看起來很相似,但輸入數據是一步之遙,也是我的一個難題。 – burkesquires

回答

1

我大量借用了this post的表格結構。

這裏的區別在於構造數組數據。通過用零初始化數組,對於每個座標(i,j),將該數組元素遞增1以表示增加後的頻率。

zip(*coords)將把所有的i組合成一個元組,並將所有的j組合在另一個元組中。通過在每個中找到最大值,我們知道數組的大小。注意,這必須從xy加大1以使其佔0,即從0到x是x + 1行。

import matplotlib.pyplot as plt 
import numpy as np 

from matplotlib.table import Table 

def table_plot(data): 
    fig, ax = plt.subplots() 
    ax.set_axis_off() 

    tb = Table(ax, bbox=[0,0,1,1]) 

    nrows, ncols = data.shape 
    width, height = 1.0/ncols, 1.0/nrows 

    for (i, j), val in np.ndenumerate(data): 
     tb.add_cell(i, j, width, height, text=str(val) if val else '', loc='center') 

    for i in range(data.shape[0]): 
     tb.add_cell(i, -1, width, height, text=str(i), loc='right', 
        edgecolor='none', facecolor='none') 
    for i in range(data.shape[1]): 
     tb.add_cell(-1, i, width, height/2, text=str(i), loc='center', 
        edgecolor='none', facecolor='none') 

    tb.set_fontsize(16) 
    ax.add_table(tb) 
    return fig 

coords = ((1,2), (2,5), (1,2), (5, 5), (4, 5)) 

# get maximum value for both x and y to allocate the array 
x, y = map(max, zip(*coords)) 
data = np.zeros((x+1, y+1), dtype=int) 

for i, j in coords: 
    data[i,j] += 1 

table_plot(data) 
plt.show() 

輸出:

enter image description here

+0

感謝您的回答。你這麼好!我編輯的問題說,我正在尋找csv輸出,但在行和列。我不需要線路。所以很抱歉混淆! – burkesquires

+0

感謝您的持續幫助。我希望能夠計算元組(1,2)的頻率並獲取顯示行和列中頻率的csv文件。這更清楚嗎? – burkesquires

+0

@burkesquires我想你想要[this]這樣的東西(http://stackoverflow.com/questions/6081008/dump-a-numpy-array-into-a-csv-file)。從座標構造的2D數組應該與我的代碼示例中的相同。你還應該記住,問題的內容不應該與原來的問題有很大的變化,因爲它會使任何當前的答案無效。這裏的問題和答案應該集中在圖中顯示2D座標。如果您對無法解決的csv文件有疑問,則應該創建一個新問題。 – Reti43

1

假設你(年,差異)元組是在一個叫做列表樣品如下

import random 
samples = [(random.randint(0,10), random.randint(0,10)) for i in range(100) ] 

的例子在本其它計算器職位描述就可以得到每對頻率How to count the frequency of the elements in a list?

import collections 
counter=collections.Counter(samples) 

形象化這個頻率表,可以將其轉換爲numpy的矩陣,並使用matshow的M atplotlib

import numpy as np 
import matplotlib.pyplot as plt 

x_max = max([x[0] for x in samples]) 
y_max = max([x[1] for x in samples]) 
freq = np.zeros((x_max+1, y_max+1)) 
for coord, f in counter.iteritems(): 
    freq[coord[0]][coord[1]] = f 
plt.matshow(freq, cmap=plt.cm.gray) 
plt.show() 
+0

感謝您的幫助。我已經嘗試了一些版本的櫃檯。當我找到上面的解決方案時,出現以下錯誤:AttributeError:'Counter'對象沒有屬性'iteritems' – burkesquires

+1

@burkesquires如果您使用的是Python 3,則語法已更改爲'counter.items()'。 'iteritems()'方法只在Python 2中存在。 – Reti43