ItGo.me Focus on IT Recommend

Home > Python Interpolation with matplotlib/basemap

Python Interpolation with matplotlib/basemap

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

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

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.

python matplotlib interpolation netcdf matplotlib-basemap
|
  this question
edited May 28 '15 at 17:44 asked May 28 '15 at 15:18 Will.Evo 107 10 1   If you don't need to use Python, a good (and easy) way to regrid is through NCL's functions: ncl.ucar.edu/Applications/regrid.shtml –  N1B4 May 28 '15 at 16:09      NOTICE: the line cs=m.pcolormesh(xi, yi, np.squeeze(smooth)) Should have ionst in place of smooth....that smooth was an attempt to interpolate and I forgot to remove the variable –  Will.Evo May 28 '15 at 16:16 1   Where can I download this file? sec_giptie_cpl_mar_120.nc Thanks. –  tommy.carstensen Jan 21 '16 at 1:15

 | 

1 Answers
1

---Accepted---Accepted---Accepted---

To smooth things out I would use imshow rather than pcolormesh

For example :

Recommend:python - matplotlib basemap subplot overlaps

how() Problem is the subplot layouts: Without tight_layout(), the plots and texts in subplots won't overlap, but they all ended up too small and don't make full use of the figure space (Fig.1). With plt.tight_layout(), subplots take up mo

from pylab import *

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

plt.show()

gives :

and

imshow(data, interpolation='bicubic')

gives :

The help page gives a list of all possible interpolations : http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow


|
  this answer
answered May 28 '15 at 15:38 Anthony Lethuillier 420 2 15      You are a genius! Holy cow the quality is insanely better without even applying any interpolation. Thanks!! –  Will.Evo May 28 '15 at 17:29      @Will.Evo: It actually applies the interpolation internally. Note the interpolation='bicubic' argument. –  cfh May 28 '15 at 20:58      Well I didn't explicitly write the interpolation argument, I just changed to imshow and it smoothed out. –  Will.Evo May 28 '15 at 21:35

 | 

Recommend:python - Matplotlib Basemap animation

o it. It could literally serve no purpose at all, as long as it is an animation it would be nice. This is what I currently have to make the map, from mpl_toolkits.basemap import Basemapimport matplotlib.pyplot as pltimport numpy as npimp

------splitte line----------------------------