【问题标题】:Python Interpolation with matplotlib/basemap使用 matplotlib/basemap 进行 Python 插值
【发布时间】:2015-08-11 04:53:09
【问题描述】:

我对编程比较陌生,很难理解插值。我发现试图解释它的每一个来源都非常神秘(尤其是底图/matplotlib 的包特定站点)。我正在使用 matplotlib 的底图进行映射,但是我的数据的性质是它以 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。

我对我用来插入数据的方法非常公正,我真的需要有人像我一样愚蠢地向我解释这一点。

另外请注意,我正在学习绘制诸如标签之类的细节,我还不太担心。下面是上面代码输出的示例地图。

【问题讨论】:

  • 如果你不需要使用 Python,一个很好(也很简单)的 regrid 方法是通过 NCL 的函数:ncl.ucar.edu/Applications/regrid.shtml
  • 注意:行 cs=m.pcolormesh(xi, yi, np.squeeze(smooth)) 应该用 ionst 代替 smooth .... 平滑是插值的尝试,我忘记了删除变量
  • 我在哪里可以下载这个文件? sec_giptie_cpl_mar_120.nc 谢谢。

标签: python matplotlib interpolation netcdf matplotlib-basemap


【解决方案1】:

为了解决问题,我会使用 imshow 而不是 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

【讨论】:

  • 你是个天才!天哪,即使没有应用任何插值,质量也会好得多。谢谢!!
  • @Will.Evo:它实际上在内部应用了插值。注意interpolation='bicubic' 参数。
  • 好吧,我没有明确写插值参数,我只是改成 imshow 并且它平滑了。
【解决方案2】:

这包含一些额外的代码,但这是我认为我最终得到的。这是几年前的事了,所以我不能 100% 确定这是上面解决我答案的确切代码。

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-18
    • 2016-12-13
    • 2012-11-29
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    相关资源
    最近更新 更多