How to create such a grid table using matplotlib

I am new to using matplotlib. I am trying to create a 2D grid using matplotlib. This is my first time using matplotlib for something non-trivial.

I decided to break the task into 3 parts:

  • Create a grid table (shown below), color the corresponding columns and correctly mark the axis. This is what I most need help with. My initial idea is to store table data in a dictionary list (or list of lists); the data structure could contain some metadata about which columns were colored, and then I could just create a matplot graph from this data, but I did not conspire with matplotlib and could get started with some help.

  • Select a character (for example, "X") in the grid cell with coordinates (row, column)

  • Save the grid table as an image (it's easy, I can do it myself)

Here is an image of the grid table I'm looking to create using matplotlib:

matplotlib grid table

I would be very grateful for any help that will make me start.

PS: the image is not very well reflected. The horizontal lines in the table have the same weight (i.e., Thickness), so the visual effect of the grid table should look like an Excel sheet.

[[Edit / Update]]

Just to clarify what I'm trying to create, this is a kind of chess board. I managed to modify the Ricardo code snippet published in his answer so that it is as close as possible (with my limited matplotlib skills!) To this game board as I can. However, there are a few things that are "missing":

  • The designations of the x-axis columns are strings, not numbers, they are string labels, for example. AB1, AB2, AB3, etc. In addition, these marks are midpoints (i.e., they are centered or lie BETWEEN with the x axis axes - not on the ticks themselves)

  • I need to write characters in a specific column for a given y axis value, for example, I can write the text "foo" with the y axis value of -1565.5 in the column "AB2".

Once I have this, I’m sure I can hack something together to get into the game I'm trying to write, especially since I just bought a copy of Matplotlib for Python developers.

+3
source share
1 answer

Mmh ... I think you could achieve this by asking matplotlib to show the grid and combining the barcode (to color the columns) using a scatter plot or direct text drawing for the character (s)

Edit: this can help you get started. However, you need work on ticks.

#!/usr/bin/python from pylab import * import matplotlib import matplotlib.ticker as ticker # Setting minor ticker size to 0, globally. # Useful for our example, but may not be what # you want, always matplotlib.rcParams['xtick.minor.size'] = 0 # Create a figure with just one subplot. # 111 means "1 row, 1 column, 1st subplot" fig = figure() ax = fig.add_subplot(111) # Set both X and Y limits so that matplotlib # don't determine it own limits using the data ax.set_xlim(0, 800) # Fixes the major ticks to the places we want (one every hundred units) # and removes the labels for the majors: we're not using them! ax.xaxis.set_major_locator(ticker.FixedLocator(range(0, 801, 100))) ax.xaxis.set_major_formatter(ticker.NullFormatter()) # Add minor tickers AND labels for them ax.xaxis.set_minor_locator(ticker.AutoMinorLocator(n=2)) ax.xaxis.set_minor_formatter(ticker.FixedFormatter(['AB%d' % x for x in range(1, 9)])) ax.set_ylim(-2000,6500, auto = False) # And set the grid! ax.grid(True, linestyle='-') # common attributes for the bar plots bcommon = dict( height = [8500], # Height = 6500 - (-2000) bottom = -2000, # Where to put the bottom of the plot (in Y) width = 100) # This is the width of each bar, itself # determined by the distance between X ticks # Now, we create one separate bar plot pear colored column # Each bar is a rectangle specified by its bottom left corner # (left and bottom parameters), a width and a height. Also, in # your case, the color. Three of those parameters are fixed: height, # bottom and width; and we set them in the "bcommon" dictionary. # So, we call bar with those two parameters, plus an expansion of # the dictionary. # Note that both "left" and "height" are lists, not single values. # That because each barplot could (potentially) have a number of # bars, each one with a left starting point, along with its height. # In this case, there only one pair left-height per barplot. bars = [[600, 'blue'], [700, 'orange']] for left, clr in bars: bar([left], color=clr, **bcommon) show() 
+4
source

Source: https://habr.com/ru/post/953533/


All Articles