gpt4 book ai didi

python - Matplotlib : display array values with imshow

转载 作者:IT老高 更新时间:2023-10-28 21:02:26 29 4
gpt4 key购买 nike

我正在尝试使用 matplotlib 函数(如 imshow)创建网格。
从这个数组:

[[ 1  8 13 29 17 26 10  4],
[16 25 31 5 21 30 19 15]]

我想将值绘制为颜色和文本值本身(1,2,...)在同一个网格上。这就是我目前所拥有的(我只能绘制与每个值相关的颜色):

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]])
print 'Here is the array'
print grid

fig1, (ax1, ax2)= pyplot.subplots(2, sharex = True, sharey = False)
ax1.imshow(grid, interpolation ='none', aspect = 'auto')
ax2.imshow(grid, interpolation ='bicubic', aspect = 'auto')
pyplot.show()

最佳答案

您想遍历 grid 中的值,并使用 ax.text 将标签添加到绘图中。

幸运的是,对于二维数组,numpyndenumerate ,这使得这很简单:

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

enter image description here

关于python - Matplotlib : display array values with imshow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33828780/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com