Python 插值 matplotlib/basemap
Python Interpolation with matplotlib/basemap
我是编程新手,很难理解插值。我能找到的每一个试图解释它的来源都非常神秘(尤其是 basemap/matplotlib 的包特定站点)。我正在使用 matplotlib 的底图进行映射,但是我的数据的性质是它以 5 度乘 5 度的块(lat lon 块)形式出现。我想通过插值来平滑地图。
所以首先是我的代码。
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。
我对我用来插入数据的东西非常公正,我只是真的需要有人向我解释这个,就像我很笨一样。
另请注意,我正在学习绘制标签等详细信息,目前我还不太担心。下面是上面的代码输出的示例地图。
为了解决问题,我会使用 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
这包含一些额外的代码,但这是我认为我最终得到的。这是几年前的事了,所以我不能 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()
我是编程新手,很难理解插值。我能找到的每一个试图解释它的来源都非常神秘(尤其是 basemap/matplotlib 的包特定站点)。我正在使用 matplotlib 的底图进行映射,但是我的数据的性质是它以 5 度乘 5 度的块(lat lon 块)形式出现。我想通过插值来平滑地图。
所以首先是我的代码。
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。
我对我用来插入数据的东西非常公正,我只是真的需要有人向我解释这个,就像我很笨一样。
另请注意,我正在学习绘制标签等详细信息,目前我还不太担心。下面是上面的代码输出的示例地图。
为了解决问题,我会使用 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
这包含一些额外的代码,但这是我认为我最终得到的。这是几年前的事了,所以我不能 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()