How to use matplotlib to draw a table-plt.table()

1. Prepare the data

import matplotlib.pyplot as plt
import numpy as np
#列名
col=[]
for i in range(1,8):
    col.append("Day"+str(i))
#行名
row=[]
for i in range(1,13):
    row.append(i)
#表格里面的具体值
vals=np.random.rand(12,7)

Column name (day of week)
Insert picture description here
row name
Insert picture description here

2. Draw a table

plt.figure(figsize=(20,8))
tab = plt.table(cellText=vals, 
              colLabels=col, 
             rowLabels=row,
              loc='center', 
              cellLoc='center',
              rowLoc='center')
tab.scale(1,2) 
plt.axis('off')

Be sure to pay attention to the last sentence of the code above, otherwise you will fail.

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43391414/article/details/114708817