【问题标题】:Matplotlib table plot, how to add gap between the graph and tableMatplotlib 表格绘图,如何在图表和表格之间添加间隙
【发布时间】:2015-12-17 04:19:46
【问题描述】:

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

这是my code

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()

结果如下:

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

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    你应该使用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])
    

    【讨论】:

    • 例如你可以使用这个位置:bbox=[0.1, -0.3, 0.9, 0.2]
    • 太棒了!请提供一个参考,我也可以在其中找到bbox 文档(或解释它们是什么以及它们为什么以这种方式工作)。谢谢
    • @xpt 有很多关于它的信息http://matplotlib.org/
    • 感谢您的链接。不过,我仍然对它们视而不见。那么 0.1, -0.3, 0.9, 0.2 这四个值到底是干什么用的呢?为什么选择这些值(即为什么它们优于 0.25、-0.5、0.5、0.3)?哪一个控制了图表和表格之间的差距?谢谢。
    • 第一个坐标是 x 轴上的偏移,第二个坐标是绘图和文本框(在您的情况下为表格)之间的间隙,第三个坐标是文本框的宽度,第四个坐标是文本框的高度。