Matplotlib:imshow中的显示元素索引

时间:2017-03-16 12:04:22

标签: python arrays numpy matplotlib grid

From this answer我知道如何绘制显示数组值的图像。 但是如何显示数组中每个元素的OpenBrowser索引,而不是值本身?

这是在图像中打印值的方式:

i,j

enter image description here

现在,如何在左上角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() 制作imshow而非价值(0,0)

你改变了什么?
1

1 个答案:

答案 0 :(得分:1)

  

如何在左上角制作imshow plot(0,0)而不是值1?

这个问题的天真答案是text(i,j,'(0,0)', ...,它将(0,0)放在每个元素上。

我想这就是你真正想要的:

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