本文介绍了生成网格单元(占用网格),颜色单元并删除xlabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何才能完全删除 Xticks,如果您查看生成的图像,它们非常模糊但仍然存在? 是否有更好的方法来管理我忽略的网格图和生成?我知道generate_moves并不完美,但这是第一个. How can I completely remove the Xticks, if you look at the images generated they are quite faint but still there?
Is there a better approach to the grid plotting and generation that I am overlooking? I know the generate_moves is not perfect but this is the first take.
我正在研究一些基于图形的规划算法,但我首先想设置一个绘图脚本,该脚本采用 2D 矩阵并将其绘制为网格单元,并根据特定值为单元着色.
我想确定两件事:
I am working on some graph based planning algorithms but I first wanted to setup a plotting script that takes a 2D matrix and plots it as grid cells, and colors the cells based on certain values.
I am trying to determine two things:
Here is a link to the repo where I have got to (same as the code below)
Here is the code I have
import matplotlib.pyplot as plt from matplotlib import colors import numpy as np import random EMPTY_CELL = 0 OBSTACLE_CELL = 1 START_CELL = 2 GOAL_CELL = 3 MOVE_CELL = 4 # create discrete colormap cmap = colors.ListedColormap(['white', 'black', 'green', 'red', 'blue']) bounds = [EMPTY_CELL, OBSTACLE_CELL, START_CELL, GOAL_CELL, MOVE_CELL ,MOVE_CELL + 1] norm = colors.BoundaryNorm(bounds, cmap.N) def plot_grid(data, saveImageName): fig, ax = plt.subplots() ax.imshow(data, cmap=cmap, norm=norm) # draw gridlines ax.grid(which='major', axis='both', linestyle='-', color='k', linewidth=1) ax.set_xticks(np.arange(0.5, rows, 1)); ax.set_yticks(np.arange(0.5, cols, 1)); plt.tick_params(axis='both', labelsize=0, length = 0) # fig.set_size_inches((8.5, 11), forward=False) plt.savefig(saveImageName + ".png", dpi=500) def generate_moves(grid, startX, startY): num_rows = np.size(grid, 0) num_cols = np.size(grid, 1) # Currently do not support moving diagonally so there is a max # of 4 possible moves, up, down, left, right. possible_moves = np.zeros(8, dtype=int).reshape(4, 2) # Move up possible_moves[0, 0] = startX - 1 possible_moves[0, 1] = startY # Move down possible_moves[1, 0] = startX + 1 possible_moves[1, 1] = startY # Move left possible_moves[2, 0] = startX possible_moves[2, 1] = startY - 1 # Move right possible_moves[3, 0] = startX possible_moves[3, 1] = startY + 1 # Change the cell value if the move is valid for row in possible_moves: if row[0] < 0 or row[0] >= num_rows: continue if row[1] < 0 or row[1] >= num_cols: continue grid[row[0], row[1]] = MOVE_CELL if __name__ == "__main__": rows = 20 cols = 20 # Randomly create 20 different grids for i in range(0, 20): data = np.zeros(rows * cols).reshape(rows, cols) start_x = random.randint(0, rows - 1) start_y = random.randint(0, cols - 1) data[start_x, start_y] = START_CELL goal_x = random.randint(0, rows - 1) # Dont want the start and end positions to be the same # so keep changing the goal x until its different. # If X is different dont need to check Y while goal_x is start_x: goal_x = random.randint(0, rows - 1) goal_y = random.randint(0, cols - 1) data[goal_x, goal_y] = GOAL_CELL generate_moves(data, start_x, start_y) plot_grid(data, "week1/images/grid_" + str(i))解决方案
Use the following to hide ticks and labels
def plot_grid(data, saveImageName): fig, ax = plt.subplots() ax.imshow(data, cmap=cmap, norm=norm) # draw gridlines ax.grid(which='major', axis='both', linestyle='-', color='k', linewidth=1) ax.set_xticks(np.arange(0.5, rows, 1)); ax.set_yticks(np.arange(0.5, cols, 1)); plt.tick_params(axis='both', which='both', bottom=False, left=False, labelbottom=False, labelleft=False) fig.set_size_inches((8.5, 11), forward=False) plt.savefig(saveImageName + ".png", dpi=500)更多推荐
生成网格单元(占用网格),颜色单元并删除xlabel
发布评论