Python quiver and pcolormesh not lining up exactly right

柔情痞子 提交于 2019-12-24 11:29:57

问题


I am trying to overlay a quiver plot of wind field on a map with a pcolormesh of the windspeed.

from mpl_toolkits.basemap import Basemap
from pylab import * 

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,llcrnrlat=latMin,urcrnrlon=lonMax,urcrnrlat=latMax, resolution = 'i')

m.pcolormesh(lonGrid, latGrid, sqrt(u**2+v**2))
m.quiver(lonGrid,latGrid,u,v, latlon = 'true')
m.drawcoastlines()
m.fillcontinents()

This gives me something like the below plot

Two things I notice:

  1. The quiver arrows emanate from the lower left corner of the pixels generated from pcolormesh, rather than the center
  2. the upper and right-most border of the plot isn't colored

What exactly is going on here, and how do I fix it?


回答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:



来源:https://stackoverflow.com/questions/40899350/python-quiver-and-pcolormesh-not-lining-up-exactly-right

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