0

I have been trying to present a table of data in python. I've been generating the table using the matplotlib pyplot module. Unfortunately the data sets I want to present are quite large. Hence when the table displays I either get it showing the entire table, but the data is too tiny to read, or it shows the data at readable size, but cuts off the rest of the table.

My first thought was perhaps if I got the table formatted in a readable way I could then use standard pan/zoom button in the interactive navigation. However clicking and dragging around the screen doesn't seem to shift the table at all. I have tried this on pycharm and anaconda, just in case it made a difference for some reason.

Thus I am wondering, once I format the table in a readable way, how can I pan around the table? Otherwise, are there any other ways to present large amounts of data in tables using python?

Also please note that I want the table to be shown when the code is executed, not saved as an image.

Some test code I have been working with trying to solve this issue:

import numpy as np
import matplotlib.pyplot as plt

data=np.random.rand(100, 1)
cols=("column")
nrows, ncols = len(data)+1, len(cols)
hcell = 0.2
wcell = 1.0
hpad, wpad = 0, 0
fig=plt.figure(figsize=(ncols*wcell+wpad, nrows*hcell+hpad))
ax =fig.add_subplot(111)
ax.axis('off')
cellText=data
table=ax.table(cellText=cellText, colLabels=cols, loc='cent')
plt.tight_layout()
plt.show()
leob
  • 141
  • 2
  • 7
  • Can you specify why exactly you want to use python and matplotlib to produce a table? If this is about sending tabular data to someone, why not use OpenDocumentTables or pdf? So maybe you can go into more detail about what characteristics of your project make it necessary to "present large amounts of data in tables **using python**"? – ImportanceOfBeingErnest Oct 19 '16 at 17:06

1 Answers1

2

Try with Tabulate module, which is very simple to use and support numpy:

tabulate module

sample code to start with:

import numpy as np
import matplotlib.pyplot as plt

from tabulate import tabulate
data=np.random.rand(100, 1)

print tabulate(data)

Using matplotlib:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.rand(100, 1)

colLabels=("Fist Column")
nrows, ncols = len(data)+1, len(colLabels)
hcell, wcell = 0.1, 0.1 # tweak as per your requirements
hpad, wpad = 0.5, 0.5    
fig=plt.figure(figsize=(ncols*wcell+wpad, nrows*hcell+hpad))
ax = fig.add_subplot(111)
ax.axis('off')

the_table = ax.table(cellText=data,
          colLabels=colLabels,
          loc='center')

plt.show()

References:

  1. https://stackoverflow.com/a/26937531/2575259
  2. Creating tables in matplotlib
  3. In Matplotlib, what does the argument mean in fig.add_subplot(111)?
Community
  • 1
  • 1
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • thanks! I wasn't aware of this module. It doesn't seem to be one of the default modules that comes with anaconda or pycharm though? I want to stick with default modules if possible so my data is viewable for anyone I send it to – leob Oct 19 '16 at 14:51