Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is a screenshot of the output :

output

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading