Python – Matplotlib table doesn't show of the rows that are expected

Question:

I am trying to create a table using matplotlib in Python.

Here is the code

# -*- coding: utf-8 -*-
import sys
import matplotlib.pyplot as plt 

val1 = ["ABC", "CDE", "AEX", "SM12", "AEX", "AEX", "AEX", "AEX", "AEX", "AEX", "AEX", "AEX", "AEX", "ABC", "AEF", "AEF", "SC", "SV", "JAVA", "TBS", "AEF", "AEF", "AEF", "ABC"]
val2 = ["KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "KPG", "LOL"]
val3 = [["" for c in range(24)] for r in range(35)] 
def cration_tableau():
    creation_of_table = True
    fig, ax = plt.subplots() 
    ax.set_axis_off() 
    table = ax.table( 
        cellText = val3,  
        rowLabels = val2,  
        colLabels = val1, 
        rowColours = ["palegreen"] * 35,  
        colColours = ["palegreen"] * 24, 
        cellColours = [["grey"] * 24] * 35,
        cellLoc ='center',  
        loc ='upper left') 
    ax.set_title('DATA TABLE', fontweight ="bold") 
    plt.show()
    
def main():
    tab = cration_tableau()

if __name__ == '__main__':
   main()

The problem is that the number of rows stops at the index of 24, when in fact I have 35 rows with values in the val2 list. The table doesn’t show all the rows that are expected.
Is there a way to make the table take in consideration all the rows ?

Here is a screenshot of the output :

output

Asked By: user12304an

||

Answers:

The table does have 35 rows, it is just a display issue ; last rows are plotted out of the window.

Either specify an adequate figure size when creating the figure:

 fig, ax = plt.subplots(figsize=(10, 8))

Or make a call to tight_layout before displaying the figure:

fig.tight_layout()
plt.show()

table

Answered By: paime
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.