Search code examples
pythonpython-2.7matplotlib

Matplotlib table falls outside plot area


I have a table within a plot using Matplotlib and the table spills out past the plot area. Is there a way to make this not happen, perhaps with an offset or something similar?

import matplotlib.pylab as plt

plt.figure()
ax=plt.gca()

col_labels=['col1','col2','col3']
row_labels=['row1','row2','row3']
table_vals=[[11,12,13],[21,22,23],[31,32,33]]
the_table = plt.table(cellText=table_vals,
                  colWidths = [0.1]*3,
                  rowLabels=row_labels,
                  colLabels=col_labels,
                  loc='center left')
plt.text(12,3.4,'Table Title',size=8)


plt.show()

Solution

  • Add a bbox parameter, in this way you can control the exact location, for example:

    plt.table(cellText=table_vals,
                      colWidths = [0.1]*3,
                      rowLabels=row_labels,
                      colLabels=col_labels,
                      loc='center left',
                      bbox=[0.25, -0.5, 0.5, 0.3])
    

    Here is the corresponding documentation for table and the one for bbox.