xpt
xpt

Reputation: 22994

Matplotlib table plot, how to add gap between the graph and table

How to add gap between the graph and table when plotting table in Matplotlib?

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

and the result looks like this:

plt.table

I.e., the table header is in the way of x-labels.
How to add gap between the graph and table? Thanks

Upvotes: 6

Views: 11322

Answers (1)

Vasya  Pravdin
Vasya Pravdin

Reputation: 154

You should use bbox argument:

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

Upvotes: 9

Related Questions