matplotlib 中的两个表

Two tables in matplotlib

我正在尝试将两个表放在 matplotlib 中的同一张图上。到目前为止我得到的唯一解决方案是使用 loc 选项:

T1 = plt.table(cellText=A, loc='left', rowLabels=names1, colLabels=names2,\
               cellColours=plt.cm.coolwarm(normal(A)))
T2 = plt.table(cellText=B, loc='right', rowLabels=names1, colLabels=names2,\
               cellColours=plt.cm.coolwarm(normal(B)))

这导致两个表格彼此相距太远并被图形边界切断。

有没有办法手动控制桌子之间的距离?

您可以准确指定轴的坐标,然后可以将每个 table 放在单独的轴中。请参阅以下示例:

left, width = 0.1, 0.6
bottom, height = 0.1, 0.8
left_table = left+width+0.1
table_width = 0.15
table_height = width/2.

rect_main = [left, bottom, width, height]
rect_table1 = [left_table, table_height+bottom , table_width, table_height]
rect_table2 = [left_table, bottom, table_width, table_height]

axMain = plt.axes(rect_main)
axTable1 = plt.axes(rect_table1, frameon =False)
axTable2 = plt.axes(rect_table2, frameon =False)
axTable1.axes.get_xaxis().set_visible(False)
axTable2.axes.get_xaxis().set_visible(False)
axTable1.axes.get_yaxis().set_visible(False)
axTable2.axes.get_yaxis().set_visible(False)

axMain.plot([1,2,3])
axTable1.table(cellText=[[1,1],[2,2]], loc='upper center',
               rowLabels=['row1','row2'], colLabels=['col1','col2'])
axTable2.table(cellText=[[3,3],[4,4]], loc='upper center',
               rowLabels=['row1','row2'], colLabels=['col1','col2'])

输出如下:

或者,您可以使用子图功能为您安排坐标轴:

axMain = subplot(1,2,1)
axTable1 = subplot(2,2,2, frameon =False)
axTable2 = subplot(2,2,4, frameon =False)
setp(axTable1, xticks=[], yticks=[]) # another way of turning off ticks
setp(axTable2, xticks=[], yticks=[]) # another way of turning off ticks

axMain.plot([1,2,3])
axTable1.table(cellText=[[1,1],[2,2]], loc='upper center',
               rowLabels=['row1','row2'], colLabels=['col1','col2'])
axTable2.table(cellText=[[3,3],[4,4]], loc='upper center',
               rowLabels=['row1','row2'], colLabels=['col1','col2'])

结果是: