Python Quiver And Pcolormesh Not Lining Up Exactly Right
Solution 1:
There are a few problems with your code.
First of all, avoid using from pylab import *
, that will pollute your namespace horribly.
Secondly, the missing data on top and to the right: this is due to the behaviour of pcolormesh
, which mimics the MATLAB function of the same name. Quoting the documentation of the functionally similar pcolor
where this is explained:
pcolor(X, Y, C, **kwargs)
[...]
Ideally the dimensions of X and Y should be one greater than those of C; if the dimensions are the same, then the last row and column of C will be ignored.
So you can get rid of the empty borders by using an auxiliary array of latitudes/longitudes. Alternatively, I suggest using imshow
, the basemap version of which automatically adjusts the scale such that the plotted image spans the visible map. Switching your pcolormesh
call to
m.imshow(sqrt(u**2+v**2),interpolation='none')
you get
Now, the last issue is how you are trying to visualize your data. What is your data? In the above plot, data points correspond to the bottom right of each "pixel", that is where your (lat,lon)
points are situated. So the current visualization is such: each arrow starts from the point it corresponds to, and each pixel corresponds to data on its lower left corner.
What you want to do is somehow shift those arrows into the center of pixels. If you want to be precise, you actually need to shift the pixels, since the quiver plot is by definition where it should be. The other option is to leave your map as-is, and shift the quiver plots (the rationale behind this version is that you discretize your data, and on a pixel scale it doesn't matter where you put the arrows/pixels).
Since in my opinion it is more exact if your quiver arrows stay where they are, I suggest shifting the whole basemap by half a (lat,lon)
unit so that pixels are centered on actual data points. You can make it prettiest by passing the pivot='middle'
option to quiver
: in this case your arrows will be centered on data points (this situated in the middle of each pixel) rather than originating from said points:
from mpl_toolkits.basemap import Basemap
#from pylab import *
from pylab import arange,meshgrid,random,sqrt
lonMin = 115.5
lonMax = 124.5
latMin = 10
latMax = 20
res = 0.25
lonGrid = arange(lonMin, lonMax, res)
latGrid = arange(latMin, latMax, res)
lonGrid,latGrid = meshgrid(lonGrid,latGrid)
u = random(lonGrid.shape)
v = random(lonGrid.shape)
m = Basemap(llcrnrlon=lonMin-res/2,llcrnrlat=latMin-res/2,
urcrnrlon=lonMax-res/2,urcrnrlat=latMax-res/2,
resolution='i') # shifted!# data corresponds to (latGrid,lonGrid)# basemap plot is shifted with (-res/2,-res/2)# imshow will automatically use the visible map anyway
m.imshow(sqrt(u**2+v**2), interpolation='none')
m.quiver(lonGrid,latGrid,u,v, latlon='true', pivot='middle')
m.drawcoastlines()
m.fillcontinents()
The resulting plot looks fairly nice, and now it is also obvious that colour is related to the magnitude of the arrows:
Post a Comment for "Python Quiver And Pcolormesh Not Lining Up Exactly Right"