1

From this answer I know how to plot an image showing the array values. But how to show the i,j indices of each element of the array, instead of the values themselves?

This is how the values are printed in the image:

from matplotlib import pyplot
import numpy as np

grid = np.array([[1,8,13,29,17,26,10,4],[16,25,31,5,21,30,19,15]])

fig1, (ax1, ax2)= pyplot.subplots(2, sharex = True, sharey = False)
ax1.imshow(grid, interpolation ='none', aspect = 'auto')
ax2.imshow(grid, interpolation ='bicubic', aspect = 'auto')
for (j,i),label in np.ndenumerate(grid):
    ax1.text(i,j,label,ha='center',va='center')
    ax2.text(i,j,label,ha='center',va='center')
pyplot.show() 

enter image description here

Now, how do you make imshow plot (0,0) in the upper left corner instead of the value 1? What do you change in

for (j,i),label in np.ndenumerate(grid):
        ax1.text(i,j,label,ha='center',va='center')
        ax2.text(i,j,label,ha='center',va='center')
Community
  • 1
  • 1
FaCoffee
  • 7,609
  • 28
  • 99
  • 174
  • Appologies if I presume, but this seems a very trivial question considering your Python score. How come you did not work this out for yourself? – MB-F Mar 16 '17 at 12:28
  • I appreciate your questioning, but you don't have to be judgmental. My Python score was assigned to me by other peers, just like yours. You know that the Python world is vast, and it takes a while to master it. The people, especially if they are learning the language by trying to solve the problems they have in their field, will be skewed towards one area rather than another. – FaCoffee Mar 16 '17 at 12:44
  • Sorry, I meant no offense and neither intended to question your reputation. On the contrary, I am genuinely curious what prevented you from applying a little try-and-error? Why, it almost looks like you copied the code from that other answer and want others to adapt it for you. I would not expect such behavior from an experienced community member, so I just have to ask. – MB-F Mar 16 '17 at 17:20

1 Answers1

2

Build a string-valued label from the coordinates i and j, like this:

from matplotlib import pyplot
import numpy as np

grid = np.array([[1,8,13,29,17,26,10,4],[16,25,31,5,21,30,19,15]])

fig1, (ax1, ax2)= pyplot.subplots(2, sharex = True, sharey = False)
ax1.imshow(grid, interpolation ='none', aspect = 'auto')
ax2.imshow(grid, interpolation ='bicubic', aspect = 'auto')
for (j, i), _ in np.ndenumerate(grid):
    label = '({},{})'.format(j, i)
    ax1.text(i,j,label,ha='center',va='center')
    ax2.text(i,j,label,ha='center',va='center')
pyplot.show() 

enter image description here

MB-F
  • 22,770
  • 4
  • 61
  • 116