custom matplotlib plot : chess board like table with colored cells


Question

I am starting to render plots with matplotlib as I learn both python and this interesting plotting library. I need help with a custom plot for a problem I am working on. May be there is an inbuilt function already for this.

Problem: I am trying to draw a table(rectangle) as a plot with 96 individual cells ( 8 rows X 12 cols). Color each alternative cell with a specific color ( like a chess board : instead of black/white I will use some other color combination) and insert value for each cell from a pandas data frame or python dictionary. Show the col and row labels on the side.

Sample Data: http://pastebin.com/N4A7gWuH

I would like the plot to look something like this substituting the values in the cells from a numpy/pandas ds.

Sample Plot: http://picpaste.com/sample-E0DZaoXk.png

Appreciate your input.

PS: did post the same on mathplotlib's mailing list

1
13
1/4/2017 10:19:36 PM

Accepted Answer

Basically, you can just use imshow or matshow.

However, I'm not quite clear what you mean.

If you want a chessboard with every "white" cell colored by some other vector, you could do something like this:

import matplotlib.pyplot as plt
import numpy as np

# Make a 9x9 grid...
nrows, ncols = 9,9
image = np.zeros(nrows*ncols)

# Set every other cell to a random number (this would be your data)
image[::2] = np.random.random(nrows*ncols //2 + 1)

# Reshape things into a 9x9 grid.
image = image.reshape((nrows, ncols))

row_labels = range(nrows)
col_labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
plt.matshow(image)
plt.xticks(range(ncols), col_labels)
plt.yticks(range(nrows), row_labels)
plt.show()

enter image description here

Obviously, this only works for things with and odd number of rows and columns. You can iterate over each row for datasets with an even number of rows and columns.

E.g.:

for i, (image_row, data_row) in enumerate(zip(image, data)):
    image_row[i%2::2] = data_row

However, the number of "data" cells in each row is going to be different, which is where I get confused by your problem definition.

By definition, a checkerboard pattern has a different number of "white" cells in each row.
Your data presumably (?) has the same number of values in each row. You need to define what you want to do. You can either truncate the data, or add an extra column.

Edit: I just realized that that's true only for odd-length numbers of columns.

Regardless, I'm still confused by your question.

Do you want have a "full" grid of data and want to set a "checkerboard" pattern of values in the data grid to a different color, or do you want to "intersperse" your data with a "checkerboard" pattern of values plotted as some constant color?

Update

It sounds like you want something more like a spreasheet? Matplotlib isn't ideal for this, but you can do it.

Ideally, you'd just use plt.table, but in this case, it's easier to use matplotlib.table.Table directly:

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

from matplotlib.table import Table

def main():
    data = pandas.DataFrame(np.random.random((12,8)), 
                columns=['A','B','C','D','E','F','G','H'])
    checkerboard_table(data)
    plt.show()

def checkerboard_table(data, fmt='{:.2f}', bkg_colors=['yellow', '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

    # 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]

        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)
    return fig

if __name__ == '__main__':
    main()

enter image description here

34
4/17/2012 9:21:13 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon