Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7796621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:25:28+00:00 2026-06-01T23:25:28+00:00

I am starting to render plots with matplotlib as I learn both python and

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:25:29+00:00Added an answer on June 1, 2026 at 11:25 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Starting this morning, if I try to write in the following method using autocomplete
Starting this morning my Eclipse indigo have projects and files with gray light color.
Starting from this Question I discovered that using the powershell.exe.config to load .net Framework
I Just starting out w/ ASP.NET MVC 3 and I am trying to render
Just starting out to program in python and I'm having the following issue. I
I'm just starting now a small project so I can learn more of the
I have an RSS feed from Zendesk (http://mysupport.zendesk.com/forums/some-path/posts.rss) that I need to render as
I'm starting out with Backbone.JS, and have a question about how this should work.
I'm starting to wonder if this is even possible. I've searched for solutions on
I'm trying to render simple 3d shapes (in this particular case, a cube) in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.