Home > OS >  how to turn an mxn array of letters into a grid image with the letters inside of each grid (python)
how to turn an mxn array of letters into a grid image with the letters inside of each grid (python)

Time:07-25

I have an array of m x n dimensions, eg [[a,b,c],[d,e,f],[g,h,i]]. What I need to do is create an grid image where each letter is in the middle of each image (see image). Any clues as to how I can do this? I've tried matplotlib grid, but there doesn't seem to be a way to put the letters in the grid.

Enter image description here:

enter image description here

CodePudding user response:

array = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]

rows = len(array)
columns = len(array[0])
fontHeight = 20
fontWidth = 20

for row in range(rows 1):
    plt.plot([0, columns*fontWidth],
            [row*fontHeight, row*fontHeight], color="black")

for column in range(columns 1):
    plt.plot([column*fontWidth, column*fontWidth],
            [0, rows*fontHeight], color="black")

for row in range(rows):
    for column in range(columns):
        plt.text((1/2 column)*fontWidth, (rows-row-1/2)*fontHeight,
                array[row][column], fontsize=fontHeight, ha="center", va="center")

plt.show()

enter image description here

  • Related