文章目录
- 1、前言
- 2、代码
- 2.1、指北针
- 2.2、比例尺
- 3、结果
1、前言
- 地理信息绘制中添加指北针和比例尺,使得图像更专业。
2、代码
2.1、指北针
def add_north(ax, labelsize=18, loc_x=0.95, loc_y=0.99, width=0.06, height=0.09, pad=0.14):"""画一个比例尺带'N'文字注释主要参数如下:param ax: 要画的坐标区域 Axes实例 plt.gca()获取即可:param labelsize: 显示'N'文字的大小:param loc_x: 以文字下部为中心的占整个ax横向比例:param loc_y: 以文字下部为中心的占整个ax纵向比例:param width: 指南针占ax比例宽度:param height: 指南针占ax比例高度:param pad: 文字符号占ax比例间隙:return: None"""minx, maxx = ax.get_xlim()miny, maxy = ax.get_ylim()ylen = maxy - minyxlen = maxx - minxleft = [minx + xlen*(loc_x - width*.5), miny + ylen*(loc_y - pad)]right = [minx + xlen*(loc_x + width*.5), miny + ylen*(loc_y - pad)]top = [minx + xlen*loc_x, miny + ylen*(loc_y - pad + height)]center = [minx + xlen*loc_x, left[1] + (top[1] - left[1])*.4]triangle = mpatches.Polygon([left, top, right, center], color='k')ax.text(s='N',x=minx + xlen*loc_x,y=miny + ylen*(loc_y - pad + height),fontsize=labelsize,horizontalalignment='center',verticalalignment='bottom')ax.add_patch(triangle)
2.2、比例尺
- add_scalebar
#-----------函数:添加比例尺--------------
def add_scalebar(ax,lon0,lat0,length,size=0.45):'''ax: 坐标轴lon0: 经度lat0: 纬度length: 长度size: 控制粗细和距离的'''# style 3ax.hlines(y = lat0, xmin = lon0, xmax = lon0+length/111, colors="black", ls="-", lw=1, label='%d km' % (length))ax.vlines(x = lon0, ymin = lat0-size, ymax = lat0+size, colors="black", ls="-", lw=1)ax.vlines(x = lon0+length/2/111, ymin = lat0-size, ymax = lat0+size, colors="black", ls="-", lw=1)ax.vlines(x = lon0+length/111, ymin = lat0-size, ymax = lat0+size, colors="black", ls="-", lw=1)ax.text(lon0+length/111,lat0+size+0.05,'%d' % (length),horizontalalignment = 'center')ax.text(lon0+length/2/111,lat0+size+0.05,'%d' % (length/2),horizontalalignment = 'center')ax.text(lon0,lat0+size+0.05,'0',horizontalalignment = 'center')ax.text(lon0+length/111/2*3,lat0+size+0.05,'km',horizontalalignment = 'center')# style 1# ax.hlines(y=lat0, xmin = lon0, xmax = lon0+length/111, colors="black", ls="-", lw=2, label='%d km' % (length))# ax.vlines(x = lon0, ymin = lat0-size, ymax = lat0+size, colors="black", ls="-", lw=2)# ax.vlines(x = lon0+length/111, ymin = lat0-size, ymax = lat0+size, colors="black", ls="-", lw=2)# # ax.text(lon0+length/2/111,lat0+size,'500 km',horizontalalignment = 'center')# ax.text(lon0+length/2/111,lat0+size,'%d' % (length/2),horizontalalignment = 'center')# ax.text(lon0,lat0+size,'0',horizontalalignment = 'center')# ax.text(lon0+length/111/2*3,lat0+size,'km',horizontalalignment = 'center')# style 2# ax.hlines(y=lat0, xmin = lon0, xmax = lon0+length/111, colors="black", ls="-", lw=1, label='%d km' % (length))# ax.vlines(x = lon0, ymin = lat0-size, ymax = lat0+size, colors="black", ls="-", lw=1)# ax.vlines(x = lon0+length/111, ymin = lat0-size, ymax = lat0+size, colors="black", ls="-", lw=1)# ax.text(lon0+length/111,lat0+size,'%d km' % (length),horizontalalignment = 'center')# ax.text(lon0,lat0+size,'0',horizontalalignment = 'center')
- draw_the_scale
def draw_the_scale(ax, y,x,text,length = 1.5,lw = 5):#画比例尺函数# y代表比例尺所在纬度# x代表比例尺开始的经度# text代表比例尺最后刻度值# length代表比例尺的长度,单位为多少个经度# lw代表比例尺的宽度step = length/5#计算步长,画五格#画黑白线五条ax.hlines(y=y,xmin=x,xmax=x + step,colors="black", ls="-", lw=lw)ax.hlines(y=y,xmin=x + step,xmax=x + step*2,colors="white", ls="-", lw=lw)ax.hlines(y=y,xmin=x + step*2,xmax=x + step*3,colors="black", ls="-", lw=lw)ax.hlines(y=y,xmin=x + step*3,xmax=x + step*4,colors="white", ls="-", lw=lw)ax.hlines(y=y,xmin=x + step*4,xmax=x + step*5,colors="black", ls="-", lw=lw)#画长刻度两个ax.vlines(x = x, ymin = y - (lw/100) *3, ymax = y + lw/100, colors="black", ls="-", lw=1)ax.vlines(x = x + length, ymin = y - (lw/100) *3, ymax = y + lw/100, colors="black", ls="-", lw=1)#画段刻度四个ax.vlines(x = x + step, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)ax.vlines(x = x + step*2, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)ax.vlines(x = x + step*3, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)ax.vlines(x = x + step*4, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)#写字,0,500,kmax.text(x,y + (lw/100) *7,'0',horizontalalignment = 'center')ax.text(x + length,y + (lw/100) *7,text,horizontalalignment = 'center')ax.text(x + length/2,y + (lw/100)*2,'km',horizontalalignment = 'center')
- draw_the_scale改
import matplotlib.patches as mpatches
def draw_the_scale(ax, y=0,x=0,length=500,lw=5):#画比例尺函数# y代表比例尺所在纬度# x代表比例尺开始的经度# text代表比例尺最后刻度值# length代表比例尺的长度,单位为多少个经度# lw代表比例尺的宽度step_ = length/111 #计算步长,画五格step = step_ * 100/111 /5#画黑白线五条ax.hlines(y=y,xmin=x,xmax=x + step,colors="black", ls="-", lw=lw)ax.hlines(y=y,xmin=x + step,xmax=x + step*2,colors="white", ls="-", lw=lw)ax.hlines(y=y,xmin=x + step*2,xmax=x + step*3,colors="black", ls="-", lw=lw)ax.hlines(y=y,xmin=x + step*3,xmax=x + step*4,colors="white", ls="-", lw=lw)ax.hlines(y=y,xmin=x + step*4,xmax=x + step*5,colors="black", ls="-", lw=lw)#画长刻度两个ax.vlines(x = x, ymin = y - (lw/100) *3, ymax = y + lw/100, colors="black", ls="-", lw=1)ax.vlines(x = x + step*5, ymin = y - (lw/100) *3, ymax = y + lw/100, colors="black", ls="-", lw=1)#画段刻度四个ax.vlines(x = x + step, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)ax.vlines(x = x + step*2, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)ax.vlines(x = x + step*3, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)ax.vlines(x = x + step*4, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)# #写字,0,500,kmax.text(x,y + 0.25,'0',horizontalalignment = 'center')ax.text(x + step_ -0.25,y + 0.25,str(length),horizontalalignment = 'center')ax.text(x + step_ + 0.25,y-0.25,'km',horizontalalignment = 'center')
- draw_the_scale改2
def draw_the_scale(ax, y,x,length=500,num=5, lw=5):#画比例尺函数# y代表比例尺所在纬度# x代表比例尺开始的经度# text代表比例尺最后刻度值# length代表比例尺的长度,单位为多少个经度# num代表要划分的线段数# lw代表比例尺的宽度step_ = length/111 # 计算经度跨度step = step_ * 100/111 /num # 100km为一格#画黑白线五条for i in range(num):if i%2 == 0:ax.hlines(y=y, xmin=x+i*step, xmax=x + (i+1)*step,colors="black", ls="-", lw=lw)else:ax.hlines(y=y,xmin=x + i*step,xmax=x + (i+1)*step,colors="white", ls="-", lw=lw)if i > 0:ax.vlines(x = x + (i+1)*step, ymin = y - (lw/100) *2, ymax = y + lw/100, colors="black", ls="-", lw=1)#画长刻度两个ax.vlines(x = x, ymin = y - (lw/100) *3, ymax = y + lw/100, colors="black", ls="-", lw=1)ax.vlines(x = x + step*num, ymin = y - (lw/100) *3, ymax = y + lw/100, colors="black", ls="-", lw=1)# #写字,0,500,kmax.text(x,y + 0.25,'0',horizontalalignment = 'center')ax.text(x + step_ -0.25,y + 0.25,str(length),horizontalalignment = 'center')ax.text(x + step_ + 0.25,y-0.25,'km',horizontalalignment = 'center')
3、结果