python - matplotlib 中的两个表

标签 python 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'])

其输出如下所示: enter image description here

或者,您可以使用子图功能来为您排列轴:

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'])

结果是: enter image description here

关于python - matplotlib 中的两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28505315/

相关文章:

python - 对不平衡数据集进行重新采样的数据框

python - 在 Python 中创建自定义对象,仅保存数据

python - Robot Framework - 将库函数作为参数传递

python - 如何使用 Matplotlib 在直方图中按组填充颜色?

python - 询问matplotlib streamplot "The rows of ' x' 必须等于错误”

python - Python中非常大的数字的二项式测试

python - PyInstaller 未找到预编译引导加载程序且构建失败

python - imshow 上/下三角形周围的边框

Python 实现我自己的 linspace 绘图例程

python - PySide6.1 与 matplotlib 3.4 不兼容