python 绘制封装函数绘制南海小地图
生活随笔
收集整理的這篇文章主要介紹了
python 绘制封装函数绘制南海小地图
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
使用python封裝函數(shù)為每一幅子圖上添加小地圖,以中國南海為例,繪制結(jié)果如下所示:
其中,主要封裝了兩個函數(shù),一個用來繪制小地圖,一個用來添加海洋、陸地、湖泊、河流等屬性,而圖中多個子圖通過for 循環(huán)進行繪制。
代碼如下:
import matplotlib.pyplot as pltimport matplotlib.transforms as mtransformsimport cartopy.crs as ccrsimport cartopy.feature as cfeatureimport numpy as npdef adjust_sub_axes(ax_main,ax_sub, shrink):''' 將ax_sub調(diào)整到ax_main的右下角.shrink指定縮小倍數(shù)。當ax_sub是GeoAxes時,需要在其設(shè)定好范圍后再使用此函數(shù)'''bbox_main = ax_main.get_position()bbox_sub = ax_sub.get_position()ratio = bbox_main.width / bbox_sub.widthwnew = bbox_sub.width* ratio * shrinkhnew = bbox_sub.height*ratio* shrinkbbox_new = mtransforms.Bbox.from_extents(bbox_main.x1 - wnew,bbox_main.y0,bbox_main.x1, bbox_main.y0 + hnew)ax_sub.set_position(bbox_new)def map(ax):ax.coastlines()ax.add_feature(cfeature.OCEAN)ax.add_feature(cfeature.LAND, edgecolor='black')ax.add_feature(cfeature.LAKES, edgecolor='black')ax.add_feature(cfeature.RIVERS)return ax ### 設(shè)置投影、畫板、子圖個數(shù)、子圖區(qū)域 proj = ccrs.PlateCarree() fig = plt.figure(figsize=(10,8),dpi=100) subplot_kw = {'projection': proj} axes_main = fig.subplots(2, 2, subplot_kw=subplot_kw) axes_sub = fig.subplots(2, 2, subplot_kw=subplot_kw) box_main =[59,151, 0,60] box_sub = [116,124,20,28] ### 循環(huán)繪制子圖 for a_main,a_sub in zip(axes_main.flat, axes_sub.flat):ax1=a_mainax1=map(ax1)gl=ax1.gridlines(draw_labels=True, xlocs=[60,90,120,150],ylocs=[10,30,50])gl.xlabels_top = Nonegl.ylabels_right = Falseax1.set_extent(box_main, crs=proj)#===================a_sub=================================================ax2=a_subax2=map(ax2)ax2.set_extent(box_main, crs=proj)adjust_sub_axes(a_main,a_sub,shrink=.3)plt.show()簡單記錄一下,可能還有許多細節(jié)沒有處理到位,歡迎交流~
總結(jié)
以上是生活随笔為你收集整理的python 绘制封装函数绘制南海小地图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ OpenCV生成九宫格图像
- 下一篇: html 应用程序主机 自动关闭,服务器