Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为numpy矩阵生成干净的x轴和y轴?_Python_Arrays_Numpy_Matrix - Fatal编程技术网

Python 如何为numpy矩阵生成干净的x轴和y轴?

Python 如何为numpy矩阵生成干净的x轴和y轴?,python,arrays,numpy,matrix,Python,Arrays,Numpy,Matrix,我正在用numpy创建一个距离矩阵,输出如下: ['H', 'B', 'D', 'A', 'I', 'C', 'F'] [[ 0. 2.4 6.1 3.2 5.2 3.9 7.1] [ 2.4 0. 4.1 1.2 3.2 1.9 5.1] [ 6.1 4.1 0. 3.1 6.9 2.8 5.2] [ 3.2 1.2 3.1 0. 4. 0.9 4.1] [ 5.2 3.2 6.9 4. 0. 4.7 7.9

我正在用numpy创建一个距离矩阵,输出如下:

  ['H', 'B', 'D', 'A', 'I', 'C', 'F']
[[ 0.   2.4  6.1  3.2  5.2  3.9  7.1]
 [ 2.4  0.   4.1  1.2  3.2  1.9  5.1]
 [ 6.1  4.1  0.   3.1  6.9  2.8  5.2]
 [ 3.2  1.2  3.1  0.   4.   0.9  4.1]
 [ 5.2  3.2  6.9  4.   0.   4.7  7.9]
 [ 3.9  1.9  2.8  0.9  4.7  0.   3.8]
 [ 7.1  5.1  5.2  4.1  7.9  3.8  0. ]]
在打印实际矩阵a之前,我只需打印一个列表即可打印x轴:

print" ", names
print a

我需要按照这个顺序排列轴,因为列表“名称”正确地排列了变量及其在矩阵中的值。但是我怎样才能在numpy中获得类似的y轴呢?

它不是很漂亮,但这个漂亮的表格可以打印:

import numpy as np

names=np.array(['H', 'B', 'D', 'A', 'I', 'C', 'F'])
a=np.array([[ 0.,   2.4,  6.1,  3.2,  5.2,  3.9,  7.1],
 [2.4,  0.,   4.1,  1.2,  3.2,  1.9,  5.1],
 [6.1,  4.1,  0.,   3.1,  6.9,  2.8,  5.2],
 [3.2,  1.2,  3.1,  0.,   4.,   0.9,  4.1],
 [5.2,  3.2,  6.9,  4.,   0.,   4.7,  7.9],
 [3.9,  1.9 , 2.8,  0.9,  4.7,  0.,   3.8],
 [7.1,  5.1,  5.2,  4.1,  7.9,  3.8,  0. ]])

def pptable(x_axis,y_axis,table):
    def format_field(field, fmt='{:,.2f}'):
        if type(field) is str: return field
        if type(field) is tuple: return field[1].format(field[0])
        return fmt.format(field)     

    def get_max_col_w(table, index):
        return max([len(format_field(row[index])) for row in table])         

    for i,l in enumerate(table):
        l.insert(0,y_axis[i])

    x_axis.insert(0,' ')    
    table.insert(0,x_axis)    
    col_paddings=[get_max_col_w(table, i) for i in range(len(table[0]))]
    for i,row in enumerate(table):
        # left col
        row_tab=[str(row[0]).ljust(col_paddings[0])]
        # rest of the cols
        row_tab+=[format_field(row[j]).rjust(col_paddings[j]) 
                for j in range(1,len(row))]
        print(' '.join(row_tab))         

x_axis=['x{}'.format(c) for c in names]
y_axis=['y{}'.format(c) for c in names]

pptable(x_axis,y_axis,a.tolist()) 
印刷品:

     xH   xB   xD   xA   xI   xC   xF
yH 0.00 2.40 6.10 3.20 5.20 3.90 7.10
yB 2.40 0.00 4.10 1.20 3.20 1.90 5.10
yD 6.10 4.10 0.00 3.10 6.90 2.80 5.20
yA 3.20 1.20 3.10 0.00 4.00 0.90 4.10
yI 5.20 3.20 6.90 4.00 0.00 4.70 7.90
yC 3.90 1.90 2.80 0.90 4.70 0.00 3.80
yF 7.10 5.10 5.20 4.10 7.90 3.80 0.00

如果希望X轴和Y轴相同,只需使用两个相同标签的列表来调用它

你什么意思?是否要在阵列左侧沿y方向打印标识符(如电子表格)?或者你只是想在上面打印y标识符,然后在下面打印数组的转置(因为数组/矩阵是对称的,在这种情况下也会显示相同的情况)?我想在数组的左边,沿着y方向打印标识符,就像我在数组的顶部,在x方向打印一样。使用包含我需要放置的特定顺序的列表“名称”。是的,x和y标识符都是相同的,因为它是一个对称矩阵,所以它基本上是一个反射。