1

I have a matrix stored as a list of lists, and two more lists representing the labels for x and 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 to draw the matrix as a table (like the picture below).

Is it possible in Python?

Matrix

1

1 Answer 1

5

I think that you can just use the plt.text for those purposes. The code below uses it to obtain the result you want.

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()

And, you get

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.