Python教程

用python画出网格图与路线图

本文主要是介绍用python画出网格图与路线图,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

 

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import MultipleLocator
import copy
import pylab
import random

network = [[0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0],
           [0,1,1,0,1,0,1,1,1,0,0,0,1,1,1,1,0,0,0,0],
           [0,1,1,0,0,0,1,1,1,0,0,0,1,1,1,1,0,0,0,0],
           [0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0],
           [0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,0,0],
           [1,1,1,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0],
           [1,1,1,1,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0],
           [1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0],
           [1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0],
           [0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0],
           [0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,1,1,1,0],
           [0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,1,1,1,0],
           [0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0],
           [0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0],
           [0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0],
           [1,1,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0],
           [1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1],
           [0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,1,1,0],
           [0,0,0,0,0,0,1,1,1,1,1,1,0,0,1,0,0,0,0,0],
           [0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0]]

def return_axis(path):# 将坐标转换为axis
    path_x = []
    path_y = []
    for item in path:
        path_x.append(item[0])
        path_y.append(item[1])
    return path_x, path_y

def draw(network, path):
    n = len(network)
    network_draw = np.zeros((n,n))
    for i in range(n):# 换色
        for j in range(n):
            if network[i][j] == 0:
                network_draw[i][j] = 1
            else:
                network_draw[i][j] = 0
    mat = np.array(network_draw)
    plt.matshow(mat, cmap=plt.cm.gray)# 着色,黑白灰
    x = np.array(range(n))
    y = x
    for item in path:
        pylab.scatter(item[0], item[1],color='green')# 画点
    path_x, path_y = return_axis(path)
    plt.plot(path_x, path_y, 'g-', color='green')# 画折线
    # pylab.xticks(x)
    # pylab.yticks(y)
    # plt.grid()

    plt.show()

def accord(network, point, dx, dy, path):# 下一跳符合矩阵条件,且不在路径中、不为墙
    n = len(network)
    # print(point,dx,dy,path)
    next_x = point[0] + dx
    next_y = point[1] + dy
    # print([next_x, next_y],path,True)
    if next_x >= 0 and next_x < n and next_y >=0 and next_y < n and \
        network[next_x][next_y] != 1 and [next_x, next_y] not in list(path):
        return True
    return False

path = [[0,0],[1,0],[2,0],[3,0],[3,1],[3,2],[3,3],[3,4],[4,4],[5,4],[5,5],
        [5,6],[5,7],[6,7],[7,7],[8,7],[9,7],[10,7],[11,7],[12,7],[13,7],
        [13,8],[13,9],[13,10],[13,11],[13,12],[13,13],[13,14],[14,14],
        [15,14],[15,15],[15,16],[15,17],[15,18],[15,19],[16,19],[17,19],
        [18,19],[19,19]]
draw(network, path)

 

这篇关于用python画出网格图与路线图的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!