python - 无法使用 matplotlib 绘制图形
问题描述
我一直试图在网格上找到最短距离的两个点。我有2个问题:
1)我无法想出一个有效的算法来找到点之间的路径。
2)我正在处理4个不同的点。0是起点,1是障碍物,2是终点,3是访问过的点。我希望已访问的点变为红色。但是当我这样做时,超过一半的图表变成了红色。
这是我的代码:
import matplotlib as mpl
from matplotlib import pyplot
import numpy as np
grid = [[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,2,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],
]
def search(x, y):
if grid[x][y] == 2:
print ('found at %d,%d' % (x, y))
return True
elif grid[x][y] == 1:
#print ('wall at %d,%d' % (x, y))
return False
elif grid[x][y] == 3:
print ('visited at %d,%d' % (x, y))
return False
#print ('visiting %d,%d' % (x, y))
# mark as visited
grid[x][y] = 3
# explore neighbors clockwise starting by the one on the right
if ((x < len(grid)-1 and search(x+1, y))
or (y > 0 and search(x, y-1))
or (x > 0 and search(x-1, y))
or (y < len(grid)-1 and search(x, y+1))):
return True
return False
search(0, 0)
zvals = grid
ax = pyplot.gca()
major_ticks = np.arange(0.5, 20, 1)
pyplot.xticks(rotation=90)
ax.set_xticks(major_ticks)
ax.set_yticks(major_ticks)
ax.grid(which='both')
pyplot.grid(True)
# make a color map of fixed colors
cmap = 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 used
img = pyplot.imshow(zvals,interpolation='nearest',
cmap = cmap,norm=norm)
# make a color bar
pyplot.colorbar(img,cmap=cmap,
norm=norm,boundaries=bounds,ticks=[0,1])
pyplot.show()
解决方案
由于算法本身运行良好,我想唯一的问题是显示结果的正确性。我建议在网格上使用尽可能多的不同颜色,这里有 4 种不同的颜色。
# make a color map of fixed colors
cmap = mpl.colors.ListedColormap(['white','black','red','limegreen'])
bounds=[-.5,0.5,1.5,2.5,3.5]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
# tell imshow about color map so that only set colors are used
img = pyplot.imshow(zvals,interpolation='nearest', norm=norm, cmap=cmap)
# make a color bar
pyplot.colorbar(img, ticks=list(range(4)))
推荐阅读
- javascript - 选项卡不在焦点时进度条停止
- swift - 相机应用程序 - 使用带有 .addOnly PHAccessLevel 的 PHPhotoLibrary 创建新相册
- php - 在 PHP 多处理中获得响应
- c# - NodeJS 和 Serialport – 读取 RFID 卡
- windows - Windows Mobile 6.1 编程
- latex - Latex:表枚举
- terraform - “列表”参数的值无效:列表元素类型不正确:需要字符串
- python - Pandas groupby cumsum 从最后到第一个
- r-package - R中cox回归的变量选择
- vim - 如何在vim中将行剪切到剪贴板?不是拉扯或复制,而是切割