1

I have changed the code by Joe Kington and wrote the following:

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

from matplotlib.table import Table

def main():
    MY_List=[[1,2,5,56,6,7,7,7,7,5],[3,4,5,5,5,5,5,5,5,8], [4,5,6,6,7,7,4,3,4,2],[4,7,2,3,4,5,6,78,8,9]]
    data = pandas.DataFrame(MY_List)

    checkerboard_table(data)
    plt.show()


def checkerboard_table(data, fmt='{:.2f}', bkg_colors=['C', 'white']):
    fig, ax = plt.subplots()
    ax.set_axis_off()
    tb = Table(ax, bbox=[0,0,1,1])

    nrows, ncols = data.shape
    width, height = 1.0 / ncols, 1.0 / nrows
    print(data)

    # Add cells
    for (i,j), val in np.ndenumerate(data):
        # Index either the first or second item of bkg_colors based on
        # a checker board pattern
        idx = [j % 2, (j + 1) % 2][i % 2]
        color = bkg_colors[idx]
        #print(i,j,val)
        #print(data)

        tb.add_cell(i, j, width, height, text=fmt.format(val),
                    loc='center', facecolor=color)

    # Row Labels...
    for i, label in enumerate(data.index):
        tb.add_cell(i, -1, width, height, text=label, loc='right',
                    edgecolor='none', facecolor='none')
    # Column Labels...
    for j, label in enumerate(data.columns):
        tb.add_cell(-1, j, width, height/2, text=label, loc='center',
                           edgecolor='none', facecolor='none')
    ax.add_table(tb)
    plt.savefig("hk.png")
    return fig

if __name__ == '__main__':
    main()

This code gives me a table containing MY_list matrix as this table. I need to change MY_list matrix with a "for loop" to have a matrix with different sizes (some of nested lists contain more than 3000*2000 items). This huge size of matrix makes my table cells very very tiny and so I can't see any number inside my cells and even in some cases cells are so tiny that hardly can be seen!

I need to increase automatically the size of my tables depending on the size of matrix without changing the size of each cell. For example, I need each cell have a fixed size and after increasing the matrix size, this cell size doesn't get tinyier. Instead I want my plot get bigger and bigger if my matrix is getting bigger (but not decrease the size of each cell so nothing can be seen inside the cells!). If the matrix is increasing the size, I need the plot increase it by adding a scroll bar at the right and bottom of itself (like what we can see in excel so we can add a huge data there without changing cell size).

Is there anyone who can help me solve this problem? Thanks

Sara
  • 163
  • 2
  • 10
  • 2
    As a first approximation, make the figure size proportional to the number of cells in each direction. E.g. to show a n x m matrix, use `figsize=(n*0.05,m*0.05)`. Of course this is slightly inaccurate, because you need to take the border into account. Once you've done that, one can surely help to do the fine tuning if it causes problems. As for the scrollbars, see e.g. [this question](https://stackoverflow.com/questions/42622146/scrollbar-on-matplotlib-showing-page). Again, update your question with the code you have problems with, such thatn one can help. – ImportanceOfBeingErnest Dec 18 '17 at 16:55
  • I'm so grateful for your help. The problem is solved and following I added the code for others – Sara Dec 18 '17 at 18:43

1 Answers1

1

By ImportanceOfBeingErnest's help in comments, I was able to solve the problem. I decided to share my code for others:

import matplotlib
# Make sure that we are using QT5
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
from PyQt5 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar


class ScrollableWindow(QtWidgets.QMainWindow):
    def __init__(self, fig):
        self.qapp = QtWidgets.QApplication([])

        QtWidgets.QMainWindow.__init__(self)
        self.widget = QtWidgets.QWidget()
        self.setCentralWidget(self.widget)
        self.widget.setLayout(QtWidgets.QVBoxLayout())
        self.widget.layout().setContentsMargins(0,0,0,0)
        self.widget.layout().setSpacing(0)

        self.fig = fig
        self.canvas = FigureCanvas(self.fig)
        self.canvas.draw()
        self.scroll = QtWidgets.QScrollArea(self.widget)
        self.scroll.setWidget(self.canvas)

        self.nav = NavigationToolbar(self.canvas, self.widget)
        self.widget.layout().addWidget(self.nav)
        self.widget.layout().addWidget(self.scroll)

        self.show()
        exit(self.qapp.exec_())



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

from matplotlib.table import Table

def main():
    MY_List=[[1,2,5,56,6,7,7,7,7,5],[3,4,5,5,5,5,5,5,5,8],[4,5,6,6,7,7,4,3,4,2],[4,7,2,3,4,5,6,78,8,9],[3,4,5,5,6,6,6,7,77,7],[1,2,3,3,4,5,6,6,7,7],
         [3,4,4,5,5,5,4,4,4,4],[2,2,4,4,5,5,5,5,5,5],[2,2,3,3,4,4,3,2,3,3],[3,3,3,4,5,5,6,7,8,9],[1,1,2,3,4,5,6,6,7,8],[3,4,5,6,7,8,9,98,7,7]]
    data = pandas.DataFrame(MY_List)

    checkerboard_table(data)
    plt.show()


def checkerboard_table(data, fmt='{:.2f}', bkg_colors=['C', 'white']):
    MY_List=[[1,2,5,56,6,7,7,7,7,5],[3,4,5,5,5,5,5,5,5,8],[4,5,6,6,7,7,4,3,4,2],[4,7,2,3,4,5,6,78,8,9],[3,4,5,5,6,6,6,7,77,7],[1,2,3,3,4,5,6,6,7,7],
         [3,4,4,5,5,5,4,4,4,4],[2,2,4,4,5,5,5,5,5,5],[2,2,3,3,4,4,3,2,3,3],[3,3,3,4,5,5,6,7,8,9],[1,1,2,3,4,5,6,6,7,8],[3,4,5,6,7,8,9,98,7,7]]
    # create a figure and some subplots
    fig, ax = plt.subplots(figsize=(len(MY_List[0])*0.5,len(MY_List)*0.5))

    # pass the figure to the custom window
    ax.set_axis_off()
    tb = Table(ax, bbox=[0,0,1,1])

    nrows, ncols = data.shape
    width, height = 1.0 / ncols, 1.0 / nrows
    print(data)

    # Add cells
    for (i,j), val in np.ndenumerate(data):
        # Index either the first or second item of bkg_colors based on
        # a checker board pattern
        idx = [j % 2, (j + 1) % 2][i % 2]
        color = bkg_colors[idx]
        #print(i,j,val)
        #print(data)

        tb.add_cell(i, j, width, height, text=fmt.format(val),
                loc='center', facecolor=color)

    # Row Labels...
    for i, label in enumerate(data.index):
        tb.add_cell(i, -1, width, height, text=label, loc='right',
                edgecolor='none', facecolor='none')
    # Column Labels...
    for j, label in enumerate(data.columns):
        tb.add_cell(-1, j, width, height/2, text=label, loc='center',
                       edgecolor='none', facecolor='none')
    ax.add_table(tb)
    a = ScrollableWindow(fig)
    plt.savefig("hk.png")
    return fig

if __name__ == '__main__':
    main()
Sara
  • 163
  • 2
  • 10