使用matplotlib/basemap进行Python插值

4
我是新手程序员,很难理解插值。我所找到的每一个试图解释它的来源都非常晦涩(特别是basemap/matplotlib的特定站点)。我正在使用matplotlib的basemap进行地图绘制,但我的数据的性质是以5度×5度的块(纬度和经度块)形式提供的。我想通过插值来平滑地图。
那么首先是我的代码。
from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, addcyclic

#load the netcdf file into a variable
mar120="C:/Users/WillEvo/Desktop/sec_giptie_cpl_mar_120.nc"

#grab the data into a new variable
fh=Dataset(mar120,mode="r")

#assign model variable contents to python variables
lons=fh.variables['lon'][:]
lats=fh.variables['lat'][:]
test=fh.variables['NE'][:]

#specifying which time and elevation to map
ionst=test[12,0]

#close the netCDF file
fh.close()

# get rid of white stripe on map
ionst, lons=addcyclic(ionst, lons)

#map settings
m=Basemap(llcrnrlon=-180, llcrnrlat=-87.5, urcrnrlon=180, urcrnrlat=87.5,rsphere=6467997, resolution='i', projection='cyl',area_thresh=10000, lat_0=0, lon_0=0)

#Creating 2d array of latitude and longitude
lon, lat=np.meshgrid(lons, lats)
xi, yi=m(lon, lat)

#setting plot type and which variable to plot
cs=m.pcolormesh(xi,yi,np.squeeze(ionst))

#drawing grid lines
m.drawparallels(np.arange(-90.,90.,30.),labels=[1,0,0,0],fontsize=10)
m.drawmeridians(np.arange(-180.,181.,30.), labels=[0,0,0,1],fontsize=10)

#drawing coast lines
m.drawcoastlines()

#color bar
cbar=m.colorbar(cs, location='bottom', pad="10%")
cbar.set_label("Elecron Density cm-3")

#showing the plot
plt.show()

现在,我该如何轻松地插值我的数据以平滑它?我尝试调用Basemap.interp,但是我收到了一个错误,说basemap没有interp属性。
我对用于插值数据的工具并不偏爱,我只需要有人能像我是个白痴一样向我解释这个问题。
还要注意的是,我正在学习绘制地图,所以标签等细节我暂时不太担心。下面是上述代码输出的示例地图。

1
如果您不需要使用Python,重新网格化的一个好(且简单)方法是通过NCL的函数:https://www.ncl.ucar.edu/Applications/regrid.shtml - N1B4
注意:行cs=m.pcolormesh(xi,yi,np.squeeze(ionst))应该在ionst的位置上...那个smooth是一个尝试插值的变量,我忘记删除它了。 - Will.Evo
1
我在哪里可以下载这个文件?sec_giptie_cpl_mar_120.nc 谢谢。 - tommy.carstensen
2个回答

4
为了使画面更加平滑,我建议使用imshow而不是pcolormesh。
例如:
from pylab import *

data = random((3,3))
figure(1)
imshow(data, interpolation='none')

plt.show()

给出:输入图像说明 以及。
imshow(data, interpolation='bicubic')

提供:

enter image description here

帮助页面提供了所有可能的插值列表: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow


你真是个天才!哇塞,质量比插值后的还要好得多。谢谢!! - Will.Evo
@Will.Evo:它实际上在内部应用了插值。请注意interpolation='bicubic'参数。 - cfh
好吧,我并没有明确地编写插值参数,我只是改用了imshow函数,然后它就变得平滑起来了。 - Will.Evo
1
@tommy.carstensen 我在这个问题下添加了一个答案,其中包含我认为相关的代码(这是两年前的事情,所以我不能百分之百确定这正是那个)。 - Will.Evo

1

这里包含一些额外的代码,但我认为这就是我最终得到的结果。这是几年前的事情,所以我不能百分之百确定这就是解决我的答案的确切代码。

from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, addcyclic
import matplotlib.animation as animation

plt.rcParams['animation.ffmpeg_path'] = 'C:/FFMPEG/bin/ffmpeg'

file_loc = "C:/Users/Will Evonosky/Dropbox/SOARS/SOARS 2015/Data"
#load the netcdf file into a variable
mar120=file_loc+"/My Datasets To Share/SA120_Iono_Acc_WE.nc"

#grab the data into a new variable
fh=Dataset(mar120,mode="r")

#assign model variable contents to python variables
lons=fh.variables['lon'][:]
lats=fh.variables['lat'][:]
var1=fh.variables['GT'][:]

#specifying which time and elevation to map
ionst=var1[0,18,:,:]
details='(Z=6)'

#close the netCDF file
fh.close()



# get rid of white stripe on map
ionst, lons=addcyclic(ionst, lons)

#Setting figure attributes
fig=plt.figure(figsize=(15,15),frameon=False)

#map settings
m=Basemap(llcrnrlon=-180, llcrnrlat=-87.5, urcrnrlon=180, urcrnrlat=87.5,rsphere=6467997, resolution='l', projection='cyl',area_thresh=10000, lat_0=0, lon_0=0)

#Creating 2d array of latitude and longitude
lon, lat=np.meshgrid(lons, lats)
xi, yi=m(lon, lat)

#plotting data onto basemap
cs=m.imshow(ionst, interpolation=None, alpha=.8)
vert=plt.axvline(x=-75, color='black', linewidth=5)
#drawing grid lines
m.drawparallels(np.arange(-90.,90.,30.),labels=[1,0,0,0],fontsize=15)
m.drawmeridians(np.arange(-180.,181.,30.), labels=[0,0,0,1],fontsize=15)

#drawing coast lines
m.drawcoastlines()

#color bar
cbar=m.colorbar(cs, location='bottom', pad="10%")
cbar.set_label(r"Ion Drag $(cm/s^2)$", size=15)

#Title Preferences
plt.title('Ion Drag at '+details, size=25)


#Function to update the plots data
def updateax1(j):
    cs.set_array(var1[j,18,:,:])
    return cs,

#Animate the plot
ani1=animation.FuncAnimation(fig, updateax1, frames=range(24), interval=250, blit=True)
ani1.save('Iondrag_Map.mp4')

#showing the plot
plt.show()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接