Matplotlib make space between tables the same when table sizes change

Viewed 58

Im making some tables in matplotlib and I need the spacing to be consistent, the problem is that the tables vary in size.

import matplotlib.pyplot as plt


table1CellText = [[1, 2, 3, 4, 5, 6]]

table2CellText = [[1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 6]]

table3CellText = [[1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 6]]

table4CellText = [[1, 2, 3, 4, 5, 6],
                  [1, 2, 3, 4, 5, 6]]


def showPlot(table1CellText, table2CellText, table3CellText, table4CellText):
    tableHeights = [len(table1CellText), len(table2CellText), len(table3CellText), len(table4CellText)]
    fig, (ax1, ax2, ax3, ax4) = plt.subplots(ncols=1, nrows=4, gridspec_kw={'height_ratios': tableHeights}, figsize=(6.5, 4.7))

    ax1.axis("off")
    ax2.axis("off")
    ax3.axis("off")
    ax4.axis("off")

    table1 = ax1.table(cellText=table1CellText, loc="center")
    table2 = ax2.table(cellText=table2CellText, loc="center")
    table3 = ax3.table(cellText=table3CellText, loc="center")
    table4 = ax4.table(cellText=table4CellText, loc="center")


    table1.auto_set_font_size(False)
    table2.auto_set_font_size(False)
    table3.auto_set_font_size(False)
    table4.auto_set_font_size(False)


    # plt.tight_layout(pad=1)
    plt.subplots_adjust(hspace=0.1)


    # buffer = BytesIO()
    # fig.savefig(buffer, bbox_inches="tight", pad_inches=0.25)


    plt.show()





showPlot(table1CellText, table2CellText, table3CellText, table4CellText)
table2CellText.pop(0)
table2CellText.pop(0)
table2CellText.pop(0)
table2CellText.pop(0)
table2CellText.pop(0)
table2CellText.pop(0)
table2CellText.pop(0)
showPlot(table1CellText, table2CellText, table3CellText, table4CellText)

This code shows one plot, then removes some rows from the second table, then displays it again. The issue is that the spacing between the plots changes. How can I make sure the spacing stays the same even though the second table can vary in its number of rows?

0 Answers
Related