要想使用python绘制一般二维的空间数据可视化,Basemap这个库就不得不提,虽将不再进行维护,但其较完整的绘图体系,对绝大多数的地理空间数据可视化绘制已足够满足需求,下面,将结合实例对Python-Basemap的常用设置进行讲解。
Basemap 的参数介绍,我想大家结合官网及网络资源就可以很快明白,要想绘制出一幅“精美”空间可视化作品,涉及的参数很多,下面就常用几个参数进行讲解:首先,看一下下面代码及代码效果:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
#设置matplotlib全局参数
plt.rc("font", size=22)
plt.rcParams['font.family'] = 'Times New Roman'
plt.rcParams["font.weight"] = "bold"
plt.rcParams['axes.labelsize'] = 22
plt.rcParams['xtick.labelsize'] = 22
plt.rcParams['ytick.labelsize'] = 22
plt.rcParams["axes.labelweight"] = "bold"
plt.rcParams["axes.titleweight"] = "bold"
plt.figure(figsize=(8, 8))
#使用Basemap 进行绘制
m = Basemap(projection='ortho', lon_0=140, lat_0=0, resolution='c')
m.drawcoastlines()
m.drawcountries()
m.drawmapboundary(fill_color='white')
m.drawparallels(np.arange(-60.,61.,30.),labels=[1,0,0,0],dashes=[2,2])
m.drawmeridians(np.arange(-160.,200.,30.),labels=[0,0,0,1],dashes=[2,2])
plt.annotate('60\N{DEGREE SIGN}S', xy=(m(170, -62.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('30\N{DEGREE SIGN}S', xy=(m(170, -32.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('30\N{DEGREE SIGN}N', xy=(m(170, +27.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('60\N{DEGREE SIGN}N', xy=(m(170, +57.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('80\N{DEGREE SIGN}E', xy=(m(80, -2.5)) , color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('110\N{DEGREE SIGN}E', xy=(m(110, -2.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('110\N{DEGREE SIGN}E', xy=(m(110, -2.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('140\N{DEGREE SIGN}E', xy=(m(140, -2.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('170\N{DEGREE SIGN}E', xy=(m(170, -2.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('160\N{DEGREE SIGN}W', xy=(m(-160, -2.5)),color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.savefig(r'F:\DataCharm\Python-matplotlib kongjian\basemap_ortho.png',dpi = 600,
bbox_inches='tight',width = 8,height=8)
plt.show()
效果如下:
详细解释:
m = Basemap(projection='ortho', lon_0=140, lat_0=0., resolution='c')
设置投影为'ortho' 正射投影,也是我绘制大范围空间图比较喜欢用的投影之一,lon_0=140, lat_0=0 则设置投影中心,分辨率resolution 则有'c'
(原始),'l'
(低),'i'
(中),'h'
(高),'f'
(完整)或None
(如果没有使用边界),选择高分辨率则耗时更多。
m.drawcoastlines() 绘制海岸线;
m.drawcountries() 绘制国家线;
m.drawmapboundary(fill_color='white') 绘制地图边界,同时设置海洋 填充为白色(white)
m.drawparallels(np.arange(-60.,61.,30.),labels=[1,0,0,0],dashes=[2,2])
m.drawmeridians(np.arange(-160.,200.,30.),labels=[0,0,0,1],dashes=[2,2])
则规范化经纬线设置。较为常用的方法之一
代码:
plt.annotate('60\N{DEGREE SIGN}S', xy=(m(170, -62.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('30\N{DEGREE SIGN}S', xy=(m(170, -32.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('30\N{DEGREE SIGN}N', xy=(m(170, +27.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('60\N{DEGREE SIGN}N', xy=(m(170, +57.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('80\N{DEGREE SIGN}E', xy=(m(80, -2.5)) , color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('110\N{DEGREE SIGN}E', xy=(m(110, -2.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('110\N{DEGREE SIGN}E', xy=(m(110, -2.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('140\N{DEGREE SIGN}E', xy=(m(140, -2.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('170\N{DEGREE SIGN}E', xy=(m(170, -2.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('160\N{DEGREE SIGN}W', xy=(m(-160, -2.5)),color='black', fontweight='bold', xycoords='data', ha='center', va='top')
则将经纬度信息进行了自定义设置,目的为更好的显示经纬度信息。
通过添加如下代码进行图表美化:(字体颜色更改)
m.bluemarble(scale=0.5)#将 ASA的蓝色大理石图像作为地图背景
效果如下:
以上就是我在Basemap中使用较多的函数方法。
这里使用Himawari-8 静止卫星L1数据进行可视化展示 ,详细代码如下:
import pandas as pd
import numpy as np
from netCDF4 import Dataset
from matplotlib import pyplot as plt
from IPython.display import Image
from mpl_toolkits.basemap import Basemap
from matplotlib.colors import Normalize
import matplotlib
import matplotlib.cm as cm
#import seaborn as sns
from scipy.stats import linregress
from matplotlib import rcParams
plt.rc("font", size=22)
plt.rcParams['font.family'] = 'Times New Roman'
# plt.rcParams["font.family"] = "Century Schoolbook"
# rcParams['font.family'] = 'sans-serif'
plt.rcParams["font.weight"] = "bold"
plt.rcParams['axes.labelsize'] = 22
plt.rcParams['xtick.labelsize'] = 22
plt.rcParams['ytick.labelsize'] = 22
plt.rcParams["axes.labelweight"] = "bold"
plt.rcParams["axes.titleweight"] = "bold"
plt.figure(figsize=(12, 10))
nc_data_file = r'NC_H08_20190903_0300_R21_FLDK.06001_06001.nc'
nc_data = Dataset(nc_data_file)
BT_01 = Dataset(nc_data_file).variables['tbb_07'][:].data
lons = Dataset(nc_data_file).variables['longitude'][:].data
lats = Dataset(nc_data_file).variables['latitude'][:].data
lon_ed, lat_ed = np.meshgrid(lons, lats)
vmin = 100
vmax = 300
m = Basemap(projection='ortho', lon_0=140.714793, lat_0=-0.007606, resolution='c')
x,y = m(lon_ed, lat_ed)
val = BT_01
#m.pcolormesh(lon_ed,lat_ed,val , latlon=True,vmin=vmin,vmax=vmax, cmap=plt.cm.get_cmap('hsv'))
m.scatter(x, y, c=val, s=1.0, marker="s", zorder=1, vmin=vmin,vmax=vmax, cmap=plt.cm.get_cmap('hsv'), alpha=1.0)
m.colorbar(location='bottom', label='Brightness Temperature [K]')
m.drawcoastlines()
m.drawcountries()
m.drawmapboundary(fill_color='white')
m.drawparallels(np.arange(-60.,61.,30.),labels=[1,0,0,0],dashes=[2,2])
m.drawmeridians(np.arange(-160.,200.,30.),labels=[0,0,0,1],dashes=[2,2])
plt.title('Himawari-8 Brightness temperature of band 07 \n')
plt.annotate('60\N{DEGREE SIGN}S', xy=(m(170, -62.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('30\N{DEGREE SIGN}S', xy=(m(170, -32.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('30\N{DEGREE SIGN}N', xy=(m(170, +27.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('60\N{DEGREE SIGN}N', xy=(m(170, +57.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('80\N{DEGREE SIGN}E', xy=(m(80, -2.5)) , color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('110\N{DEGREE SIGN}E', xy=(m(110, -2.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('110\N{DEGREE SIGN}E', xy=(m(110, -2.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('140\N{DEGREE SIGN}E', xy=(m(140, -2.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('170\N{DEGREE SIGN}E', xy=(m(170, -2.5)), color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.annotate('160\N{DEGREE SIGN}W', xy=(m(-160, -2.5)),color='black', fontweight='bold', xycoords='data', ha='center', va='top')
plt.savefig(r'F:\DataCharm\Himawari-8 Brightness temperature of band07.png',dpi=900,bbox_inches='tight')
plt.show()
效果如下:
关键步骤:
lons = Dataset(nc_data_file).variables['longitude'][:].data
lats = Dataset(nc_data_file).variables['latitude'][:].data
lon_ed, lat_ed = np.meshgrid(lons, lats)
其中:lons和lats为一维向量数组,要想绘制空间可视化,则需使用np.meshgrid()——使向量数组生成网格点坐标矩阵,此方法在绘制空间数据中经常用到,具体含义大家可以自行百度啊
以上就是对Python 空间绘图库Basemap的简单介绍及应用,自己也不是很清楚用Basemap还是使用如genpandas、geoplot或者Basemap的继任者:Cartopy。又或者说使用较为灵活的R语言进行绘制。此外,Himawari-8数据也是自己研究论文使用的主要数据,后期也会有专门进行数据处理的教程推文。希望小伙伴们可以后台留言告知你们希望我使用哪一个进行绘制,同时,自己也会不断学习,加强空间可视化的绘制技巧。希望和你们一起学习,共同进步