Создайте ячейки сетки (сетка заполнения), цветные ячейки и удалите метки - PullRequest
1 голос
/ 16 июня 2019

Я работаю над некоторыми алгоритмами планирования на основе графиков, но сначала я хотел установить сценарий построения графика, который принимает 2D-матрицу и отображает ее как ячейки сетки, и окрашивает ячейки на основе определенных значений.

Япытаясь определить две вещи:

  1. Как я могу полностью удалить Xticks, если вы посмотрите на сгенерированные изображения, они довольно тусклые, но все еще есть?
  2. Есть ли лучший подход кпостроение сетки и генерация, которую я пропускаю?Я знаю, что generate_moves не идеален, но это первый дубль.

enter image description here Вот ссылка на репо, куда я попал (так же, каккод ниже)

Вот код, который у меня есть

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))

1 Ответ

1 голос
/ 16 июня 2019

Используйте следующее, чтобы скрыть галочки и метки

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)

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...