我正在尝试使用 matplotlib 制作 20x20 的可视网格。但是我无法调整网格的大小以适应每个框的大小。这是我的代码:import matplotlib as mplfrom matplotlib import pyplotimport numpy as npgrid = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0], ]print (grid)zvals = grid# make a color map of fixed colorscmap = mpl.colors.ListedColormap(['white','black'])bounds=[-2,-1,1,2]norm = mpl.colors.BoundaryNorm(bounds, cmap.N)# tell imshow about color map so that only set colors are usedimg = pyplot.imshow(zvals,interpolation='nearest', cmap = cmap,norm=norm)# make a color barpyplot.colorbar(img,cmap=cmap, norm=norm,boundaries=bounds,ticks=[0,1])pyplot.grid(which= 'both')pyplot.show()我希望网格是黑盒子的大小。
1 回答

杨__羊羊
TA贡献1943条经验 获得超7个赞
您可以将此部分添加到您的代码中:
ax = pyplot.gca()
major_ticks = np.arange(0.5, 20, 1)
pyplolt.xticks(rotation=90)
ax.set_xticks(major_ticks)
ax.set_yticks(major_ticks)
ax.grid(which='both')
pyplot.grid(True)
输出:
附言。通常pyplot
以这种方式导入:import matplotlib.pyplot as plt
添加回答
举报
0/150
提交
取消