我将矩阵存储为列表列表,另外两个列表代表x
和y
轴的标签。
A = [[1, 3, 4, 5, 6, 7],
[3, 3, 0, 7, 9, 2],
[1, 3, 4, 5, 6, 6]]
x = ["A", "B", "C", "E", "F", "G"]
Y = ["R", "S", "T"]
我想把矩阵画成一张桌子(如下图所示)。
在Python中可以吗?
答案 0 :(得分:4)
我认为您可以将plt.text
用于这些目的。下面的代码用它来获得你想要的结果。
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import rcParams
rcParams['font.family'] = 'serif'
rcParams['font.size'] = 16
A = [[1, 3, 4, 5, 6, 7],
[3, 3, 0, 7, 9, 2],
[1, 3, 4, 5, 6, 6]]
X = ["A", "B", "C", "E", "F", "G"]
Y = ["R", "S", "T"]
m = len(Y)
n = len(X)
plt.figure(figsize=(n + 1, m + 1))
for krow, row in enumerate(A):
plt.text(5, 10*krow + 15, Y[krow],
horizontalalignment='center',
verticalalignment='center')
for kcol, num in enumerate(row):
if krow == 0:
plt.text(10*kcol + 15, 5, X[kcol],
horizontalalignment='center',
verticalalignment='center')
plt.text(10*kcol + 15, 10*krow + 15, num,
horizontalalignment='center',
verticalalignment='center')
plt.axis([0, 10*(n + 1), 10*(m + 1), 0])
plt.xticks(np.linspace(0, 10*(n + 1), n + 2), [])
plt.yticks(np.linspace(0, 10*(m + 1), m + 2), [])
plt.grid(linestyle="solid")
plt.savefig("Table.png", dpi=300)
plt.show()
而且,你得到了