Map projection and forced interpolation

泄露秘密 提交于 2019-12-07 13:42:13

问题


I have a weird behaviour using different projections of Basemap.

The measurementgrid that I want to plot onto a worldmap is of shape [181,83]. That means I have values for each 2°/2° point, ranging from -180° - 180° longitude and -82° - 82° latitude.

from mpl_toolkits.basemap import Basemap
import numpy as np
measurementgrid = np.random.random_sample((181,83))
m = Basemap(projection='cyl',llcrnrlon=-180, llcrnrlat=-82, urcrnrlon=180, urcrnrlat=82, resolution='l')
m.drawcoastlines()
m.drawparallels(np.arange(-90,90,30),labels=[1,0,0,0])
m.drawmeridians(np.arange(-180,180,45), labels=[0,0,0,1])
data, x, y = m.transform_scalar(measurementgrid.T, lons=np.arange(-180,182,2), lats=np.arange(-82,84,2), nx = 181, ny = 83, returnxy=True, order=0)
m.imshow(data, origin='lower', interpolation='none')

Using the cylindrical projection the returned data grid equals the measurementgrid and everthing is fine. If I change the projection to "mill", the resulting interpolated data differs from its origin.

Is there a way to plot the measurement grid as it is but with respect to the changing projection?


回答1:


First off, I'd encourage you to start using pcolormesh, rather than imshow. Pcolormesh should be your go-to visualisation tool when trying to plot gridded data in boxes (as contourf is when drawing gridded data as iso-valued areas).

To get using pcolormesh, you should pass through the coordinates of the x and y corners of your data, so:

x = np.linspace(-180, 180, 182)
y = np.linspace(-90, 90, 84)
m.pcolormesh(x, y, data)

But with Basemap, you should always transform the coordinates into the map's coordinate system - ultimately this could potentially mean that both the x and y coordinates need to be 2 dimensional arrays, so we do that and convert:

x = np.linspace(-180, 180, 182)
y = np.linspace(-90, 90, 84)
x, y = np.meshgrid(x, y)
converted_x, converted_y = m(x, y)
m.pcolormesh(converted_x, converted_y, data)

What that means is that you can now go ahead and change the projection, and your data will be plotted in the right place. For instance, I changed the projection to "robin" (Robinson) and got the following picture:

Unfortunately, pcolormesh is for contiguous blocks of data, which if you pick a projection which does not share the same central longitude (aka "lon_0") then you will get bad results. For instance, I changed the projection to projection='robin',lon_0=180 and got the following picture:

That is because the dateline is not currently handled by Basemap, and as far as I can see, without a major re-write - never will be.

The good news is that this is an area which has bothered me for a long time, so I started writing a new package to handle this, and many other quirks of mapping for scientific visualisation. The result is a new package, called cartopy, which does a lot more work for you so that things like the dateline "just work":

import numpy as np
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

x = np.linspace(-180, 180, 182)
y = np.linspace(-90, 90, 84)
measurement_grid = np.random.random_sample((83, 181)) * y[:-1, np.newaxis] ** 2

plt.axes(projection=ccrs.Robinson(central_longitude=180))
plt.pcolormesh(x, y, measurement_grid, transform=ccrs.PlateCarree())
plt.gca().coastlines()
plt.show()

Whilst I'm not suggesting that you should change to cartopy now (installation and performance are still work in progress) - it is worth knowing that the package exists, and I expect in the future will become more and more attractive as you encounter these kinds of issues. http://scitools.org.uk/cartopy/docs/latest

It is also worth pointing out that a lot of the problems that occur with scientific visualisation of gridded data come in the handling of the data, its coordinates and their underlying coordinate systems, so another package has been written which implements a data model to encapsulate all of this complex information into a single object which can then be passed around to plotting routines for simple interfacing. Again, I'd encourage to you take a look at it http://scitools.org.uk/iris/docs/latest.

HTH



来源:https://stackoverflow.com/questions/18186047/map-projection-and-forced-interpolation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!