使用matplotlib/basemap进行Python插值
我对编程还很陌生,并且很难理解插值.我能找到的每个试图解释它的原始资料都是非常神秘的(特别是底图/matplotlib的特定于软件包的站点).我正在使用matplotlib的底图进行映射,但是我的数据的性质是它以5度乘5度的块(纬度块)进来.我想通过插值对地图进行平滑处理.
I am rather new to programming and am having a very hard time understanding interpolation. Every single source I can find that attempts to explain it is extremely cryptic (especially the package specific sites for basemap/matplotlib). I am mapping using matplotlib's basemap however the nature of my data is that it comes in 5 degree by 5 degree blocks (lat lon blocks). I want to smooth out the map by interpolation.
首先,这是我的代码.
So first here is my code.
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,但是收到一条错误消息,说该底图没有属性interp.
So now, how can I easily interpolate my data to smooth it out? I have tried to call Basemap.interp however I get an error saying that basemap has no attribute interp.
我真的很公正地使用数据进行插值,我真的很需要别人像我一样愚蠢地向我解释一下.
I am really impartial to what I use to interpolate the data, I just really need someone to explain this to me like I am dumb.
还请注意,我正在学习映射的过程,因此像标签这样的细节,到目前为止还不太担心.下面是上面的代码输出的示例地图.
Also note that I am in the process of learning to map so details like labels and such I am not too worried about as of yet. Below is an example map that the code above outputs.
为了使事情顺利进行,我将使用imshow而不是pcolormesh
To smooth things out I would use imshow rather than pcolormesh
例如:
from pylab import *
data = random((3,3))
figure(1)
imshow(data, interpolation='none')
plt.show()
给出:
和
imshow(data, interpolation='bicubic')
给予:
帮助页面列出了所有可能的插值: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow
The help page gives a list of all possible interpolations : http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow