Drawing matrix plot in Python? -
i have matrix stored list of lists, , 2 more lists representing labels x
, y
axes.
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"]
i want draw matrix table (like picture below).
is possible in python?
i think can use plt.text
purposes. code below uses obtain result want.
import matplotlib.pyplot plt import numpy np matplotlib import rcparams rcparams['font.family'] = 'serif' rcparams['font.size'] = 16 = [[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)) krow, row in enumerate(a): plt.text(5, 10*krow + 15, y[krow], horizontalalignment='center', verticalalignment='center') 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()
and, get
Comments
Post a Comment