使用Python对单元格进行编号

使用Python对单元格进行编号,python,Python,我正在尝试以下程序: L = 3 r = rand(L,L) p = 0.4 z=r<p imshow(z, origin='lower', interpolation='nearest', extent=[0, L, 0, L]) L=3 r=兰特(L,L) p=0.4 z=r如果我正确理解了你的问题,我相信这会起作用: from numpy.random import rand from matplotlib.pyplot import imshow L = 3 r = ran

我正在尝试以下程序:

L = 3
r = rand(L,L)
p = 0.4
z=r<p  
imshow(z, origin='lower', interpolation='nearest', extent=[0, L, 0, L])
L=3
r=兰特(L,L)
p=0.4

z=r如果我正确理解了你的问题,我相信这会起作用:

from numpy.random import rand
from matplotlib.pyplot import imshow

L = 3
r = rand(L,L)
p = 0.4
z=r<p  

f,ax = plt.subplots()
ax.imshow(z, origin='lower', interpolation='nearest', extent=[0, L, 0, L])

for i in range(z.shape[0]):
    for j in range(z.shape[1]):
        if z[i,j]:
            text = ax.text(j+0.5, i+0.5, np.round(r[i,j],3), ha="center", va="center", color="r")
从numpy.random导入
从matplotlib.pyplot导入imshow
L=3
r=兰特(L,L)
p=0.4

z=rI打算建议使用ax.annotate()
然后我读了你的答案。
.text
.annotate
之间有区别吗?事实上,最后一行也可以是:annotation=ax.annotate(np.round(r[i,j],3),xy=(j+0.5,i+0.5),ha=“center”,va=“center”,color=“r”)。以下(以及包含的链接)提供了有关matplotlib文本支持的一些好信息。感谢您的回复。事实上,我想用1,2,3…的方式对单元格进行编号。你能给出一个你想要的精确输出的示例/图像吗?