【问题标题】:Matplotlib: display element indices in imshowMatplotlib:在 imshow 中显示元素索引
【发布时间】:2017-03-16 12:04:22
【问题描述】:

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

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

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?你有什么变化

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')

【问题讨论】:

  • 如果我假设的话,请原谅,但考虑到您的 Python 分数,这似乎是一个非常微不足道的问题。你怎么不自己解决这个问题?
  • 感谢您的提问,但您不必做出判断。我的 Python 分数是由其他同行分配给我的,就像你的一样。你知道 Python 的世界是广阔的,掌握它需要一段时间。人们,特别是如果他们通过尝试解决他们在自己领域中遇到的问题来学习语言,将倾向于一个领域而不是另一个领域。
  • 对不起,我无意冒犯,也无意质疑您的声誉。相反,我真的很好奇是什么阻止了你进行一些尝试和错误?为什么,看起来您几乎从其他答案中复制了代码,并希望其他人为您调整它。我不希望有经验的社区成员会出现这种行为,所以我只需要问一下。

标签: python arrays numpy matplotlib grid


【解决方案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() 

【讨论】:

    猜你喜欢
    • 2019-10-11
    • 1970-01-01
    • 2012-01-13
    • 2022-01-18
    • 2014-03-09
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多