1

Hello Stackoverflow community,

I'm trying to create a matplotlib table with (roughly) 135 columns and (roughly) 10 rows. Some rows contain integers while others contain rounded floats. As a minimal working example I created the following code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

cellContentsArray = np.random.random_integers(0, 100, (10, 135))

df = pd.DataFrame(cellContentsArray,dtype=object)
df.iloc[2] = np.round(np.random.random((1, 135)),2)
df.iloc[7] = np.round(np.random.random((1, 135)),2)

fig, ax = plt.subplots()
fig.patch.set_visible(False) # hide axes
ax.axis('off')
ax.axis('tight')

rowLabelsArray = ['Row label 1','Row label 2','Row label 3','Row label 4','Row label 5','Row label 6','Row label 7','Row label 8','Row label 9','Row label 10']
cellTextArray = df.values
colLabelsArray = df.iloc[0].values
tab = ax.table(cellText=cellTextArray,rowLabels=rowLabelsArray,colLabels=colLabelsArray,loc='center',cellLoc='center')
tab.auto_set_font_size(False)
tab.set_fontsize(5)
tab.scale(1.0,0.6)

fig.tight_layout()
plt.show()

This code produces the following output: Sample matplotlib table output

As you can see (or maybe not as every cell text is very tiny) the values are displayed correctly but unfortunately the space is not exploited optimally and the content is not really readable. I already tried setting different parameters with no success so far. How would I go about adjusting this table to use the available space completely while making sure that there is no overlap of text from different table cells?

Any help would be highly appreciated. Thank you very much!

Hagbard
  • 3,430
  • 5
  • 28
  • 64
  • 1
    just a suggestions: your could use pandas to display your table: https://pandas.pydata.org/pandas-docs/stable/index.html – Sharku Aug 21 '18 at 09:27
  • 1
    Thank you for your comment. As you can see from my code above, I'm already using pandas for the underlying table structure. For displaying the table I use matplotlib as I would like to add colors to the table lateron once it is readable enough. However, I'm open to new ideas. Is there any easy way to add colors to each cell with pure pandas code? – Hagbard Aug 21 '18 at 09:32
  • 1
    look here: https://stackoverflow.com/questions/28075699/coloring-cells-in-pandas – Sharku Aug 21 '18 at 09:34
  • Thanks. I'll look into this. – Hagbard Aug 21 '18 at 09:35
  • Can you plot it vertically i/o horizontally? – Reblochon Masque Aug 21 '18 at 10:27
  • How would I do that? – Hagbard Aug 21 '18 at 10:31

0 Answers0