最近需要根据有限的站位点绘制插值等值线图,在网上中文搜索一通,只发现了这货Matplot Basemap 画湖北地图、插值、等值线,要么就是对这货的转载,这货不提供数据的形式,但是基本的代码思路还是不错的,于是继续转向google英文,搜到了如下的回答,我对代码做了注释,已备别人查询,关于文中提到的数据是txt格式的,我也直接将数据贴在下面了。总结一下:在地图上绘制等值线:
- 确定基本的绘图框架;
- 获取采集数据,与地图做映射,并根据映射数据插值;scipy.interpolate.griddata包插值比较快,常用的三种插值方法为liner(基于三角形的线性插补法),cubic(基于三角形的三次插补法),nearest( 最近邻居插补法),这些方法定义了匹配数据点的曲面类型,'cubic' 方法生成平滑曲面,而 'linear' 和 'nearest' 分别具有一阶导数和零阶导数不连续。
- 根据栅格插值数据绘图
# -*- coding: utf-8 -*-
@author: Adwiy Wang
import numpy as np
import pandas as pd
from matplotlib.mlab import griddata
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
from scipy.interpolate import griddata as gd
# 设置基本图片画板
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, axisbg='w', frame_on=False)
# 提取数据
data = pd.read_csv('datam.txt', delim_whitespace=True)
norm = Normalize()
#设置地图边界值
lllon = 21
lllat = -18
urlon = 34
urlat = -8
#初始化地图
m = Basemap(
projection = 'merc',
llcrnrlon = lllon, llcrnrlat = lllat, urcrnrlon = urlon, urcrnrlat = urlat,
resolution='h')
# 将经纬度点转换为地图映射点
data['projected_lon'], data['projected_lat'] = m(*(data.Lon.values, data.Lat.values))
# 生成经纬度的栅格数据
numcols, numrows = 1000, 1000
xi = np.linspace(data['projected_lon'].min(), data['projected_lon'].max(), numcols)
yi = np.linspace(data['projected_lat'].min(), data['projected_lat'].max(), numrows)
xi, yi = np.meshgrid(xi, yi)
# 插值
x, y, z = data['projected_lon'].values, data['projected_lat'].values, data.Z.values
zi = gd(
(data[['projected_lon', 'projected_lat']]),
data.Z.values,
(xi, yi),
method='cubic')
# 设置地图细节
m.drawmapboundary(fill_color = 'white')
m.fillcontinents(color='#C0C0C0', lake_color='#7093DB')
m.drawcountries(
linewidth=.75, linestyle='solid', color='#000073',
antialiased=True,
ax=ax, zorder=3)
m.drawparallels(
np.arange(lllat, urlat, 2.),
color = 'black', linewidth = 0.5,
labels=[True, False, False, False])
m.drawmeridians(
np.arange(lllon, urlon, 2.),
color = '0.25', linewidth = 0.5,
labels=[False, False, False, True])
# 等值面图绘制
con = m.contourf(xi, yi, zi, zorder=4, alpha=0.6, cmap='jet')
# 插入测绘点
m.scatter(
data['projected_lon'],
data['projected_lat'],
color='#545454',
edgecolor='#ffffff',
alpha=.75,
s=50 * norm(data['Z']),
cmap='jet',
ax=ax,
vmin=zi.min(), vmax=zi.max(), zorder=4)
# 插入色标、名称和范围
cbar = plt.colorbar(con,orientation='horizontal', fraction=.057, pad=0.05)
cbar.set_label("Mean Rainfall - mm")
m.drawmapscale(
24., -9., 28., -13,
100,
units='km', fontsize=10,
yoffset=None,
barstyle='fancy', labelstyle='simple',
fillcolor1='w', fillcolor2='#000000',
fontcolor='#000000',
zorder=5)
plt.title("Mean Rainfall")
plt.savefig("rainfall.png", format="png", dpi=300, transparent=True)
plt.show()
数据文件:datam.txt
Lon Lat Z
32.6 -13.6 41
27.1 -16.9 43
32.7 -10.2 46
24.2 -13.6 33
28.5 -14.4 43
28.1 -12.6 33
27.9 -15.8 46
24.8 -14.8 44
31.1 -10.2 35
25.9 -13.5 24
29.1 -9.8 10
25.8 -17.8 39
33.2 -12.3 44
28.3 -15.4 46
27.6 -16.1 47
28.9 -11.1 31
31.3 -8.9 39
31.9 -13.3 45
23.1 -15.3 31
31.4 -11.9 39
27.1 -15.0 42
24.4 -11.8 15
28.6 -13.0 39
31.3 -14.3 44
23.3 -16.1 39
30.2 -13.2 38
24.3 -17.5 32
26.4 -12.2 23
23.1 -13.5 27
网友评论