PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 28, 2022

[FIXED] how to turn an mxn array of letters into a grid image with the letters inside of each grid (python)

 July 28, 2022     arrays, graph-visualization, image, python     No comments   

Issue

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


Solution

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



Answered By - Colim
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing