问题
In recent days, I have visualized my ncep reanalysis data using Basemap. It is found that only half data appears when using Basemap(projection="cyl",lon_0=0.0,lat_0=0.0, resolution="c") , but the whole data occurs when using Basemap(projection="cyl",lon_0=180.0,lat_0=0.0, resolution="c") . Furthermore, I changes the projection, but still set the central longitude to 0 degree like Basemap(projection="hammer",lon_0=0.0,lat_0=0.0, resolution="c"), but the whole data. The whole data occurs. what happens ? Your answers are appreciated very much.
回答1:
This is a common problem with basemap
. Even though the data should be cyclic in longitudinal direction, basemap
can not deal with that properly. I usually use cdo
to first order the data in the way it should be displayed using
cdo sellonlatbox,-180,180,-90,90 input.nc output.nc
cdo
also provides a python wrapper so you can use it directly in your scripts. If you don't want to use cdo
, you can use numpy
to re-arrange your data, here is a small example:
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits import basemap
fig,axes = plt.subplots(nrows=3, ncols=1)
mp1 = basemap.Basemap(ax = axes[0], projection = 'cyl', lon_0=180, lat_0=0)
mp2 = basemap.Basemap(ax = axes[1], projection = 'cyl', lon_0=0, lat_0=0)
mp3 = basemap.Basemap(ax = axes[2], projection = 'cyl', lon_0=0, lat_0=0)
for mp in mp1, mp2, mp3:
mp.drawcoastlines()
mp.drawcountries()
mp.drawmeridians(np.arange(0,360,30))
mp.drawparallels(np.arange(-90,90,30))
##some data:
lons = np.arange(0,360)
lats = np.arange(-90,91)
lons,lats = np.meshgrid(lons,lats)
data = np.sin(2*np.pi*lons/360)+np.sin(np.pi*lats/180)
##first plot
mp1.pcolormesh(lons,lats,data)
##second plot
mp2.pcolormesh(lons,lats,data)
##third plot (with longitudes re-ordered)
lons = lons%180-180*(lons//180) ##re-computing lons to be from -180 to 180
lon_order = np.argsort(lons, axis = 1) ##computing new order of lons
lat_order = np.argsort(lats, axis = 0) ## ... and lats (maybe unnecessary)
mp3.pcolormesh(lons[lat_order,lon_order],lats, data[lat_order,lon_order])
plt.show()
The result looks like this, where the first plot displays the data in the original format, the second one tries to center the data on lon_0=0
without re-ordering and the third one does the re-ordering.
Hope this helps.
回答2:
In several cases, the longitude range in the dataset are from 0-360, while basemap expects it from - 180 to 180. One can use basemap shiftgrid function to take care of this.
from mpl_toolkits.basemap import Basemap
data, lon = shiftgrid(180., data, lon, start=False) # shiftgrid
来源:https://stackoverflow.com/questions/48257845/whats-wrong-with-basemap-projection-cyl