簡體   English   中英

有沒有辦法在 python 中創建漂亮的嵌套表?

[英]Is there a way to create nice looking nested tables in python?

我有一些像這樣的矩陣格式的數據:

data = [['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
 ['', '0', '', -1, '', -2, '', -3, '', -4, '', -5, '', -6, '', -7],
 ['', '', 1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -7, -8],
 ['', -1, -2, 1, 0, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -5],
 ['', '', -2, 0, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -3, -6],
 ['', -2, -3, 0, -1, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -3],
 ['', '', -3, -1, 1, -1, 1, -2, -2, -3, -3, -4, -2, -5, -5, -4],
 ['', -3, -4, -1, -2, 1, 0, 1, 0, 0, -1, -1, -2, -2, -3, -3],
 ['', '', -4, -2, 0, 0, 2, 0, 0, -1, -1, -2, 0, -3, -3, -4],
 ['', -4, -5, -2, -3, 0, -1, 2, 1, 1, 0, 0, -1, 0, -1, -1],
 ['', '', -5, -3, -3, -1, -1, 1, 3, 0, 2, -1, -1, -1, -1, -2],
 ['', -5, -6, -3, -4, -1, -2, 1, 0, 3, 2, 2, 1, 1, 0, 0],
 ['', '', -6, -4, -4, -2, -2, 0, 0, 2, 2, 1, 1, 0, 2, -1],
 ['', -6, -7, -4, -5, -2, -3, 0, -1, 2, 1, 2, 1, 1, 0, 2]]

像這樣的標題:

X = "AGGTTGC"
Y = "ACGGTC"

我想在 python 中創建一個如下所示的表:

在此處輸入圖像描述

我試圖用 pandas dataframe 來做到這一點,我發現了多索引,但我沒有得到我想要的結果。 我也嘗試了 pyplot,但沒有多大成功。 有什么建議么?

我終於設法只用 matplotlib.pyplot 重現了圖像:

import numpy as np
import matplotlib.pyplot as plt

def plot_calculations(X, Y, data):
    X = "-" + X
    Y = "-" + Y

    column_labels = list(X)
    row_labels = list(Y)

    plt.rcParams["figure.figsize"] = (7, 7) # resize 

    fig, ax = plt.subplots()
    plt.tight_layout()
    plt.margins(0, 0)

    for i in np.arange(0, len(X), 0.5):
        if (i % 1 == 0):
            width = 2
        else:
            width = 0.5
        plt.plot([i, i], [0, len(Y)], linewidth=width, color="k")

    for i in np.arange(0, len(Y), 0.5):
        if (i % 1 == 0):
            width = 2
        else:
            width = 0.5
        plt.plot([0, len(X)],[i, i], linewidth=width, color="k")

    for i in range(0, (len(X))*2):
        for j in range(0, (len(Y))*2):
            if (i%2 == 1 and j%2 == 1):
                fw ='bold'
                fs = 12
            else:
                fw = 'normal'
                fs = 10
            plt.text(i/2+1/4, j/2+1/4, data[j][i], fontsize=fs, horizontalalignment='center',verticalalignment='center', fontweight=fw)

    ax.set_xticks(np.arange(len(X))+0.5)
    ax.set_yticks(np.arange(len(Y))+0.5)
    ax.invert_yaxis()
    ax.xaxis.tick_top()

    ax.set_xticklabels(column_labels)
    ax.set_yticklabels(row_labels)
    plt.show()


X = "AGGTTGC"
Y = "ACGGTC"

data = [['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],
 ['', '0', '', -1, '', -2, '', -3, '', -4, '', -5, '', -6, '', -7],
 ['', '', 1, -2, -2, 0, -3, -1, -4, -2, -5, -3, -6, -4, -7, -5],
 ['', -1, -2, 1, -3, 0, -4, -1, -5, -2, -6, -3, -7, -4, -8, -5],
 ['', '', -2, -3, 0, -1, -1, -1, -2, -2, -3, -3, -4, -4, -3, -5],
 ['', -2, 0, 0, -1, 0, -2, -1, -3, -2, -4, -3, -5, -4, -6, -3],
 ['', '', -3, -4, 1, -2, 1, 0, -2, 0, -3, -1, -2, -2, -5, -3],
 ['', -3, -1, -1, -1, 1, -2, 1, -3, 0, -4, -1, -5, -2, -4, -3],
 ['', '', -4, -5, 0, -3, 2, -1, 0, 1, -1, 0, 0, -1, -3, -1],
 ['', -4, -2, -2, 0, 0, 0, 2, -1, 1, -2, 0, -3, 0, -4, -1],
 ['', '', -5, -6, -3, -4, -1, -2, 3, 0, 2, 2, -1, 1, -1, 0],
 ['', -5, -3, -3, -1, -1, 1, 1, 0, 3, -1, 2, -1, 1, -2, 0],
 ['', '', -6, -7, -4, -5, -2, -3, 0, -1, 2, 1, 1, 1, 2, 0],
 ['', -6, -4, -4, -2, -2, 0, 0, 2, 2, 1, 2, 0, 1, -1, 2]]
plot_calculations(X, Y, data)

結果如下:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM