我试图在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)))
导致两个表彼此相距太远并被图形边界切割。
有没有办法手动控制表格之间的距离?
答案 0 :(得分:2)
您可以精确指定轴的坐标,然后可以将每个表放在单独的轴上。请参阅以下示例:
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'])
结果是: