来自几个np.array()的Matplotlib set_yticklabels

时间:2017-09-12 08:38:31

标签: python python-2.7 numpy matplotlib

我有两个numpy数组,prot_nameprot_nHarea,用于标记yticks

prot_name = np.array(['HEMO', 'HSA', 'EGF', 'MYO', 'LACT', 'CKMM', 'BMG', 'IGF', 'CYTC', 'IFN', 'CREA', 'IL8'])

prot_nHarea每个prot_name有12个矩阵。

我希望每个y的第一行打勾为prot_name,后跟np.sum()中矩阵的prot_nHarea。下一行的np.mean()prot_nHarea

enter image description here

现在我正在做硬标记以标记y刻度。但有没有办法迭代np.array(prot_name)后跟np.sum(np.array(prot_nHarea))np.mean(np.array(prot_nHarea))? sum和mean的值以科学模式写出,当标签重叠时,我将\n调整一下。

    ax0.set_yticklabels(["K1 - HEMO sum: "+str('{:.2e}'.format(np.sum(prot_nHarea[0])))+"\nmean: "+str('{:.2e}'.format(np.mean(prot_nHarea[0]))), 
                    "K2 - HSA sum: "+str('{:.2e}'.format(np.sum(prot_nHarea[1])))+"\nmean: "+str('{:.2e}'.format(np.mean(prot_nHarea[1])))+"\n", 
                    "\nK3 - EGF sum: "+str('{:.2e}'.format(np.sum(prot_nHarea[2])))+"\nmean: "+str('{:.2e}'.format(np.mean(prot_nHarea[2]))),
                    "K4 - MYO sum: "+str('{:.2e}'.format(np.sum(prot_nHarea[3])))+"\nmean: "+str('{:.2e}'.format(np.mean(prot_nHarea[3]))),
                    "K5 - LACT sum: "+str('{:.2e}'.format(np.sum(prot_nHarea[4])))+"\nmean: "+str('{:.2e}'.format(np.mean(prot_nHarea[4]))),
                    "K6 - CKMM sum: "+str('{:.2e}'.format(np.sum(prot_nHarea[5])))+"\nmean: "+str('{:.2e}'.format(np.mean(prot_nHarea[5]))),
                    "K7 - BMG sum: "+str('{:.2e}'.format(np.sum(prot_nHarea[6])))+"\nmean: "+str('{:.2e}'.format(np.mean(prot_nHarea[6])))+"\n",
                    "K8 - IGF sum: "+str('{:.2e}'.format(np.sum(prot_nHarea[7])))+"\nmean: "+str('{:.2e}'.format(np.mean(prot_nHarea[7]))),
                    "K9 - CYTC sum: "+str('{:.2e}'.format(np.sum(prot_nHarea[8])))+"\nmean: "+str('{:.2e}'.format(np.mean(prot_nHarea[8]))),
                    "K10 - IFN sum: "+str('{:.2e}'.format(np.sum(prot_nHarea[9])))+"\nmean: "+str('{:.2e}'.format(np.mean(prot_nHarea[9]))),
                    "K11 - CREA sum: "+str('{:.2e}'.format(np.sum(prot_nHarea[10])))+"\nmean: "+str('{:.2e}'.format(np.mean(prot_nHarea[10]))),
                    "K12 - IL8 sum: "+str('{:.2e}'.format(np.sum(prot_nHarea[11])))+"\nmean: "+str('{:.2e}'.format(np.mean(prot_nHarea[11])))], fontsize='x-small') 

1 个答案:

答案 0 :(得分:1)

您可以使用列表理解来完成此操作,利用zipenumerate同时循环遍历prot_nameprot_nHarea

import numpy as np
import matplotlib.pyplot as plt

prot_name = np.array([
    'HEMO', 'HSA', 'EGF', 'MYO',
    'LACT', 'CKMM', 'BMG', 'IGF',
    'CYTC', 'IFN', 'CREA', 'IL8'
    ])
prot_nHarea = np.random.rand(12, 100)

yticklabels = ['K{} - {} sum: {:.2e} \nmean: {:.2e}'.format(
    i, name, Harea.sum(), Harea.mean()
    ) for i, (name, Harea) in enumerate(zip(prot_name, prot_nHarea))]

fig, ax = plt.subplots()

ax.set_yticks(range(12))
ax.set_yticklabels(yticklabels)

plt.show()

enter image description here