python - Matplotlib set_yticklabels from several np.array() -


i have 2 numpy arrays, prot_name , prot_nharea, used label yticks

prot_name = np.array(['hemo', 'hsa', 'egf', 'myo', 'lact', 'ckmm', 'bmg', 'igf', 'cytc', 'ifn', 'crea', 'il8']) 

prot_nharea has 12 matrices each prot_name.

i want have first line of each y tick prot_name followed np.sum() of matrix in prot_nharea. next line has np.mean() of prot_nharea.

enter image description here

now doing hard code label y ticks. there ways iterate through np.array(prot_name) followed np.sum(np.array(prot_nharea)) , np.mean(np.array(prot_nharea))? value sum , mean written in scientific pattern , adjust label bit \n when overlapped.

    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')  

you can list comprehension, making use of zip , enumerate loop through prot_name , prot_nharea @ same time:

import numpy np import matplotlib.pyplot 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()     ) 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


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -