Matplotlib 表格图,如何在图形和表格之间添加间隙

xpt*_*xpt 6 python plot matplotlib

在 Matplotlib 中绘制表格时如何在图形和表格之间添加间隙?

这是我的代码

import pandas as pd
import matplotlib.pyplot as plt

dc = pd.DataFrame({'A' : [1, 2, 3, 4],'B' : [4, 3, 2, 1],'C' : [3, 4, 2, 2]})

plt.plot(dc)
plt.legend(dc.columns)
dcsummary = pd.DataFrame([dc.mean(), dc.sum()],index=['Mean','Total'])

plt.table(cellText=dcsummary.values,colWidths = [0.25]*len(dc.columns),
        rowLabels=dcsummary.index,
        colLabels=dcsummary.columns,
        cellLoc = 'center', rowLoc = 'center',
        loc='bottom')
# loc='top'
fig = plt.gcf()

plt.show()
Run Code Online (Sandbox Code Playgroud)

结果如下所示:

表格

即表头以x-labels的方式出现。
如何在图表和表格之间添加间隙?谢谢

Vas*_*din 6

你应该使用bbox参数:

plt.table(cellText=dcsummary.values,colWidths = [0.25]*len(dc.columns),
rowLabels=dcsummary.index,
colLabels=dcsummary.columns,
cellLoc = 'center', rowLoc = 'center',
loc='bottom', bbox=[0.25, -0.5, 0.5, 0.3])
Run Code Online (Sandbox Code Playgroud)

  • 第一个坐标是 x 轴上的位移,第二个坐标是绘图和文本框(在你的例子中是表格)之间的间隙,第三个坐标是文本框的宽度,第四个坐标是文本框的高度。 (7认同)