I want to know how to change the font color of the specified elements to mark them in the numpy array. I referenced the answer to this question: Coloring entries in an Matrix/2D-numpy array? I write the following two functions to do this. For instance, in the following example, I want to change the color of a[0,1], a[1,2], a[2,0]. In the trans(a, j, *lst) function, a is the input matrix, j is the color number, and *lst is the index of the element to be modified.
import numpy as np
a = np.array([[0,0,0],
[0,0,0],
[23.7135132,0,0],
[32.22954112,0,0],
[71.01941585,31.27384681,0]])
print(a,'\n')
def trans(a,j,*lst):
num_row = a.shape[0]
num_column = a.shape[1]
add = ''
row = []
column = []
# record the row index and column index of each input (in *lst)
for k in range(len(lst)):
row.append(lst[k][0])
column.append(lst[k][1])
# Generate the same content as 'a' and change the color
for m in range(num_row):
for n in range(num_column):
for k in range(len(lst)):
# the element whose color needs to be modified
if m == row[k] and n == column[k]:
add += "\033[3{}m{}\033[0m ".format(j, a[row[k],column[k]])
break
# the element whose color does not need to be modified
if k == len(lst)-1:
add += "\033[3{}m{}\033[0m ".format(0, a[m,n])
# There is a space at the end of "\033[3{}m{}\033[0m "
add = add.strip(' ')
add = add.split(' ')
add = np.array(add)
add = add.reshape(num_row,num_column)
return add
def show(a, row_sep=" "):
n, m = a.shape
fmt_str = "\n".join([row_sep.join(["{}"]*m)]*n)
return fmt_str.format(*a.ravel())
print(show(trans(a,1,[0,1],[1,2],[2,0])))
But the results are as follows.
As you can see, the elements cannot be aligned, so I want to know how to automatically align these elements, just like the display of numpy array. Or is there any other way to directly modify the font color of numpy array? I just want to make some special elements more conspicuous.

