栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

栅格图绘制

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

栅格图绘制

    利用PyCharm画简单的桥面栅格地图,黄色代表桥的起点,紫色代表桥的终点,红色为桥面,蓝色为水面。
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import colors

'''
# # -------------------------------------这里定义绘图函数-------------------------------------------------
'''
def drawmap(rows,cols,startpos,endpos,betpos,riverpos):
    startIndex = startpos
    goalIndex = endpos

    betIndex=betpos
    riverIndex=riverpos


    # 创建全部为空地的地图栅格,其中空地以数字1表征
    filed = np.ones((rows,cols))

    # 修改栅格地图中起始点和终点的数值,其中起点以数值4表征,终点以数值5表示
    filed[startIndex[0],startIndex[1]] = 4
    filed[goalIndex[0],goalIndex[1]] = 5


    #长江大桥用红色表示
    for index in range(len(betIndex)):
         filed[betIndex[index][0], betIndex[index][1]] = 3
    #水面用蓝色表示
    for index in range(len(riverIndex)):
             filed[riverIndex[index][0], riverIndex[index][1]] = 8

    # 绘制图像,利用matplotlib的热力图进行绘制
    # 设置色条的范围,从0~8
    #颜色的选择与上面数值有关,0-8分别对应下面颜色
    cmap = colors.ListedColormap(['none','white','black','red','yellow','magenta','green','cyan','blue'])

    # 绘图函数
    #将函数的返回值赋予fig和ax两个变量
    #例如下面的函数便是将图像划分为9*6的表格
    #该函数在这里并不改变所绘制图像的大小,而是进程框的大小
    fig, ax = plt.subplots(figsize=(9,6))

    # 绘制热力图
    # 其中vmin和vmax对应栅格地图数值的颜色与cmap一一对应
    # cbar设置false将色条设置为不可见
    sns.heatmap(filed, cmap = cmap,vmin = 0,vmax = 8, linewidths = 1.5, linecolor= 'black',ax = ax,cbar = False)

    # 设置图标题
    ax.set_title('DrawMap')
    ax.set_ylabel('rows')
    ax.set_xlabel('cols')

    # 将列标签移动到图像上方
    ax.xaxis.tick_top()
    ax.xaxis.set_label_position('top')

    # 显示图像
    plt.show()


    # 设置图标的数字个数文字,放在plt.show下面能居中
    #ax.set_xticks()设置坐标轴x轴刻度函数,例如ax.set_xticks(1,2,3,4),将刻度设置为1,2,3,4
    ax.set_xticks(np.arange(cols))
    ax.set_xticklabels(np.arange(cols))
    #同理。ax。set_yticks是用来设置y轴刻度的函数
    ax.set_yticks(np.arange(rows))
    ax.set_yticklabels(np.arange(rows))

    return filed

'''
# # -------------------------------------这是是测试的主函数-------------------------------------------------
'''
#栅格图的行数
rows =5
# 栅格图的列数
cols = 6
#画图

startpos = [2,0]
endpos = [2,5]
betpos=[2,1],[2,2],[2,3],[2,4]
riverpos=[[3,1],[3,2],[3,3],[3,4],[1,1],[1,2],[1,3],[1,4]]

filed = drawmap(rows,cols,startpos,endpos,betpos,riverpos)
print(filed)


代码运行结果:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/925538.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号