python在范围内随机偏移_python-为什么在此示例中我的网格偏移量?

我正在尝试使用matplotlib绘制一个随机占用的网格.网格看起来与块之间的偏移量是随机的:

这是代码:

import matplotlib.pyplot as plt

import numpy as np

# Make a 10x10 grid...

nrows, ncols = 10,10

# Fill the cells randomly with 0s and 1s

image = np.random.randint(2, size = (nrows, ncols))

# Make grid

vgrid = []

for i in range(nrows + 1):

vgrid.append((i - 0.5, i - 0.5))

vgrid.append((- 0.5, 9.5))

hgrid = []

for i in range(ncols + 1):

hgrid.append((- 0.5, 9.5))

hgrid.append((i - 0.5, i - 0.5))

row_labels = range(nrows)

col_labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'j']

plt.matshow(image, cmap='Greys')

for i in range(11):

plt.plot(hgrid[2 * i], hgrid[2 * i + 1], 'k-')

plt.plot(vgrid[2 * i], vgrid[2 * i + 1], 'k-')

plt.axis([-0.5, 9.5, -0.5, 9.5])

plt.xticks(range(ncols), col_labels)

plt.yticks(range(nrows), row_labels)

plt.show()

当我强制执行一个绘图区域时,似乎出现了问题.这行:

plt.axis([-0.5, 9.5, -0.5, 9.5])

另外,请随时提出更好的方法.我是pyplot的新手.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值