python - How do I print an aligned numpy array with (text) row and column labels? -


is there elegant way exploit correct spacing feature of print numpy.array 2d array, proper labels, aligns properly? example, given array 4 rows , 5 columns, how can provide array , appropriately sized lists corresponding row , header columns generate output looks this?

        b   c   d   e z [[ 85  86  87  88  89] y  [ 90 191 192  93  94] x  [ 95  96  97  98  99] w  [100 101 102 103 104]] 

if naively try:

import numpy x = numpy.array([[85, 86, 87, 88, 89], \                  [90, 191, 192, 93, 94], \                  [95, 96, 97, 98, 99], \                  [100,101,102,103,104]])  row_labels = ['z', 'y', 'x', 'w']   print "       b   c   d   e" row, row_index in enumerate(x):     print row_labels[row_index], row 

i get:

        b   c   d   e z  [85  86  87  88  89] y  [90 191 192  93  94] x  [95  96  97  98  99] w  [100 101 102 103 104] 

is there way can things line intelligently? open using other library if there better way solve problem.

assuming matrix numbers have @ 3 digits, replace last part this:

print "       b   c   d   e" row_label, row in zip(row_labels, x):     print '%s [%s]' % (row_label, ' '.join('%03s' % in row)) 

which outputs:

       b   c   d   e z [ 85  86  87  88  89] y [ 90 191 192  93  94] x [ 95  96  97  98  99] w [100 101 102 103 104] 

formatting '%03s' results in string of length 3 left padding (using spaces). use '%04s' length 4 , on. full format string syntax explained in python documentation.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -