draw matrix as a table in python
Question: I am trying to generate a data matrix with python and wish to draw it as a
table. From IPython, I found a display api and it can display the matrix as a
table. but I still prefer: 1\. remove the row index col and col index row. 2\.
the data grid should split evenly.

I am think about matplotlib but not sure how to do it.I wish to do it in the
same script so no need to paste it here and there!

    
    
    %matplotlib inline
    import numpy as np
    import pandas as pd
    from IPython.display import display, HTML
    import matplotlib.pyplot as plt
    print pd.__version__
    
    row = 6
    col = row
    matrix = np.zeros((row, col))
    for i in range(row):
        for j in range(col):
            if i == 0:
                matrix[i][j] = 1
            else:
                matrix[i][j] = matrix[i-1][j] + matrix[i][j-1]
    df = pd.DataFrame(matrix)
    display(df)
    

Current output is:

[![enter image description
here](http://i.stack.imgur.com/BH63Xm.png)](http://i.stack.imgur.com/BH63Xm.png)

### Updated

According to Brat's comments, I have updated my code for referenc:

    
    
    %matplotlib inline
    import numpy as np
    import pandas as pd
    from IPython.display import display, HTML
    import matplotlib.pyplot as plt
    
    row = 7
    col = row
    matrix = np.zeros((row, col))
    matrix = matrix.astype(int)
    for i in range(row):
        for j in range(col):
            if i == 0:
                matrix[i][j] = 1
            else:
                matrix[i][j] = matrix[i-1][j] + matrix[i][j-1]
    df = pd.DataFrame(matrix)
    #display(df)
    
    w = 5
    h = 5
    plt.figure(1, figsize=(w, h))
    tb = plt.table(cellText=matrix, loc=(0,0), cellLoc='center')
    
    tc = tb.properties()['child_artists']
    for cell in tc: 
        cell.set_height(1.0/row)
        cell.set_width(1.0/col)
    
    ax = plt.gca()
    ax.set_xticks([])
    ax.set_yticks([])
    
    plt.show()
    

Output as bleow:

[![enter image description
here](http://i.stack.imgur.com/3JVbr.png)](http://i.stack.imgur.com/3JVbr.png)


Answer: With matplotlib you might consider using `table`, for example:

    
    
    import numpy as np
    import matplotlib.pylab as pl
    
    nx = 4
    ny = 5
    data = np.random.randint(0,10,size=(ny,nx))
    
    pl.figure()
    tb = pl.table(cellText=data, loc=(0,0), cellLoc='center')
    
    tc = tb.properties()['child_artists']
    for cell in tc: 
        cell.set_height(1/ny)
        cell.set_width(1/nx)
    
    ax = pl.gca()
    ax.set_xticks([])
    ax.set_yticks([])
    

[![enter image description
here](http://i.stack.imgur.com/SgkYh.png)](http://i.stack.imgur.com/SgkYh.png)

