解决问题
在matplotlib画的图里插入表格数据。使用到pyplot和gridspec函数。
示例代码
#导入库
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['MSYH','SimHei']
from matplotlib import gridspec
#示例代码
a = list(range(10))
b = np.random.randint(1,100,10)
plt.figure(figsize=(7,7))
gs = gridspec.GridSpec(4,1)
# 画图表
ax1 = plt.subplot(gs[:3,0])
ax1.bar(a,b)
plt.xlabel('x Label')
plt.ylabel('y Label')
plt.title('title')
# 插入表格
ax2 = plt.subplot(gs[3,0])
plt.axis('off')
rowLabels = ['第一行','第二行','第三行'] # 表格行名
cellText = [[1,2,3],[4,5,6],[7,8,9]] #表格每一行数据
table = plt.table(cellText=cellText, rowLabels=rowLabels, loc='center', cellLoc='center',rowLoc='center')
table.auto_set_font_size(False)
table.set_fontsize(10) #字体大小
table.scale(1, 1.5) #表格缩放