Python:在极坐标网格上绘制块(Basemap.pcolormesh)

时间:2015-02-26 07:14:33

标签: python matplotlib-basemap

我想在极地网格上绘制颜色块。目前(如下所示)块不遵循指定的子午线和平行线。我希望这些块适合我绘制的拱形框。任何帮助都会受到赞赏。

enter image description here

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

fig = plt.figure()

cols = np.linspace(60,200,4)
rows = np.linspace(60,78,4)

ax = fig.add_subplot(1,1,1)
Z  = np.random.random( (4,4) )

m = Basemap(width=6500000, height=3000000,
        resolution='l', projection='stere',\
        lat_0=72, lon_0=130.)

### draw parallels and meridians.
m.drawparallels(rows, linewidth=0.7)
m.drawmeridians(cols, linewidth=0.7)
m.drawmapboundary()

x, y = m(*np.meshgrid(cols,rows))
m.pcolormesh(x, y, Z, vmin=0, vmax=1)

plt.tight_layout()
plt.savefig('sof_example.png')

1 个答案:

答案 0 :(得分:0)

我认为没有简单的方法可以做到这一点。我看到它的方式,python不“知道”地图投影。你绘制的不是lon / lat,而是由底图转换为轴坐标的lon / lat。 所以你可以做的就是完成你已经完成的工作但是要改进绘图网格。

这似乎可以解决问题,但它是一种“粗暴”的解决方案;-)

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

fig = plt.figure()

cols = np.linspace(60,200,4)
rows = np.linspace(60,78,4)

ax = fig.add_subplot(1,1,1)
Z  = np.random.random( (4,4) )

m = Basemap(width=6500000, height=3000000,
        resolution='l', projection='stere',\
        lat_0=72, lon_0=130.,ax=ax)

### draw parallels and meridians.
m.drawparallels(rows, linewidth=0.7)
m.drawmeridians(cols, linewidth=0.7)
m.drawmapboundary()

### set up new cols, rows and z that are on high-res grid
cols2 = np.linspace(cols[0],cols[-1],100)
rows2 = np.linspace(rows[0],rows[-1],100)
Z2    = np.zeros((100,100))

for i in range(0,cols.shape[0]-1):
   for j in range(0,rows.shape[0]-1):
      ### find corresponding cols, rows on high-res grid
      i0 = np.where(np.abs(cols2-cols[i]) == np.min(np.abs(cols2-cols[i])))[0]
      i1 = np.where(np.abs(cols2-cols[i+1]) == np.min(np.abs(cols2-cols[i+1])))[0]
      j0 = np.where(np.abs(rows2-rows[j]) == np.min(np.abs(rows2-rows[j])))[0]
      j1 = np.where(np.abs(rows2-rows[j+1]) == np.min(np.abs(rows2-rows[j+1])))[0]
      ### put Z value in new Z2 array 
      Z2[j0:j1+1,i0:i1+1] = Z[j,i]

x,y = m(*np.meshgrid(cols2,rows2))
m.pcolormesh(x,y,Z2,vmin=0,vmax=1)

plt.tight_layout()
plt.savefig('sof_example.png')

plt.show()