0

So I have a list of 16 lists of integers. How could I make a graph using matplotlib so that one would have a space of 15x15 completely filled with a label of the integer in that position? Am I asking too much or is there some way I could tweak the list? Example of a list:

[[9, 6, 13, 14, 0, 8, 10, 2, 4, 7, 1, 3, 5, 12, 15, 11],
 [26, 30, 27, 16, 17, 24, 20, 18, 29, 23, 25, 19, 28, 31, 22, 21],
  [35, 46, 37, 41, 39, 40, 33, 36, 45, 42, 47, 43, 34, 44, 32, 38],
   [58, 56, 53, 61, 54, 63, 50, 62, 49, 55, 48, 51, 57, 59, 52, 60],
    [66, 76, 69, 75, 72, 71, 73, 68, 79, 67, 65, 64, 78, 74, 77, 70],
     [80, 91, 83, 93, 85, 90, 92, 86, 95, 81, 89, 94, 84, 87, 88, 82],
      [103, 97, 98, 104, 111, 109, 102, 108, 96, 107, 110, 106, 99, 101, 105, 100],
       [115, 122, 126, 121, 127, 113, 118, 116, 125, 119, 117, 120, 112, 123, 114, 124],
        [140, 130, 141, 132, 143, 131, 133, 137, 138, 134, 129, 135, 128, 139, 142, 136],
         [158, 147, 156, 144, 157, 152, 150, 149, 154, 159, 148, 151, 153, 155, 146, 145],
          [163, 169, 175, 166, 174, 173, 165, 160, 170, 162, 167, 171, 168, 161, 164, 172],
           [187, 188, 186, 180, 182, 184, 177, 189, 185, 181, 179, 176, 183, 178, 191, 190],
            [200, 203, 204, 195, 196, 205, 206, 197, 201, 193, 194, 207, 198, 199, 192, 202],
             [218, 220, 212, 213, 222, 219, 223, 221, 215, 216, 209, 217, 211, 214, 210, 208],
              [239, 236, 233, 230, 235, 229, 226, 238, 227, 234, 231, 225, 237, 228, 232, 224],
               [254, 247, 240, 251, 241, 245, 252, 243, 255, 248, 242, 249, 250, 246, 244, 253]]

How could I plot this so that it would take up a 15x15 space with the integers labeled? (Each point would be labeled with the integer it represents in the array)

5
  • What do you mean the "label"? Commented Mar 28, 2014 at 0:28
  • I want each point to be labeled with the integer it represents Commented Mar 28, 2014 at 0:39
  • 1
    Just use text or annotate, and specify the (x,y) position of each integer. See, for example, stackoverflow.com/questions/2897826/… or stackoverflow.com/questions/5821125/… Commented Mar 28, 2014 at 4:53
  • Im not sure I understand how to apply those examples to my own list, I am very new to matplotlib & numpy Commented Mar 28, 2014 at 14:48
  • @tom10 just to alert you Commented Mar 28, 2014 at 15:49

1 Answer 1

3

You can use text to do this, as tom10 said. Here's an example.

import matplotlib.pyplot as plt

data = [[9, 6, 13, 14, 0, 8, 10, 2, 4, 7, 1, 3, 5, 12, 15, 11],
[26, 30, 27, 16, 17, 24, 20, 18, 29, 23, 25, 19, 28, 31, 22, 21],
[35, 46, 37, 41, 39, 40, 33, 36, 45, 42, 47, 43, 34, 44, 32, 38],
[58, 56, 53, 61, 54, 63, 50, 62, 49, 55, 48, 51, 57, 59, 52, 60],
[66, 76, 69, 75, 72, 71, 73, 68, 79, 67, 65, 64, 78, 74, 77, 70],
[80, 91, 83, 93, 85, 90, 92, 86, 95, 81, 89, 94, 84, 87, 88, 82],
[103, 97, 98, 104, 111, 109, 102, 108, 96, 107, 110, 106, 99, 101, 105, 100], 
[115, 122, 126, 121, 127, 113, 118, 116, 125, 119, 117, 120, 112, 123, 114, 124],
[140, 130, 141, 132, 143, 131, 133, 137, 138, 134, 129, 135, 128, 139, 142, 136],
[158, 147, 156, 144, 157, 152, 150, 149, 154, 159, 148, 151, 153, 155, 146, 145],
[163, 169, 175, 166, 174, 173, 165, 160, 170, 162, 167, 171, 168, 161, 164, 172],
[187, 188, 186, 180, 182, 184, 177, 189, 185, 181, 179, 176, 183, 178, 191, 190],
[200, 203, 204, 195, 196, 205, 206, 197, 201, 193, 194, 207, 198, 199, 192, 202],
[218, 220, 212, 213, 222, 219, 223, 221, 215, 216, 209, 217, 211, 214, 210, 208],
[239, 236, 233, 230, 235, 229, 226, 238, 227, 234, 231, 225, 237, 228, 232, 224],
[254, 247, 240, 251, 241, 245, 252, 243, 255, 248, 242, 249, 250, 246, 244, 253]]

# Create a figure and axis
fig = plt.figure()
ax = plt.subplot(111)

# Plot each number based on its position
y = 1
for row in data:
    x = 1
    for n in row:
        plt.text(x,y,str(n))
        x +=1
    y += 1
# Set the x and y axes limits
xMax = len(data)
yMax = len(data[0])    
ax.set_xlim(0, xMax + 1)
ax.set_ylim(0, yMax + 1)

plt.show()

plot of integers in a grid

Sign up to request clarification or add additional context in comments.

1 Comment

Note that instead of your nested loop, you could use ndenumerate, e.g. for (x,y), n in np.ndenumerate(data): plt.text(x+1,y+1, str(n)) (or maybe with x and y swapped, I can never remember).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.