overlaying a basemap on contours

谁说胖子不能爱 提交于 2019-12-11 10:46:29

问题


This question is a follow-up to an earlier question and from @JoeKington here. Both of these solutions work excellently for my needs. However I have been trying to overlay a basemap on the contours. Going by the example here http://matplotlib.org/basemap/users/examples.html, I do not seem to get it right. I think my basic problem is to convert the contour x,y values into map coordinates. I reproduce below the codes for 1) contours (as given by @usethedeathstar, which works very well) and 2) the map object and the plotting.

#!/usr/bin/python
from mpl_toolkits.basemap import Basemap
import numpy as np
from scipy.interpolate import griddata

class d():
    def __init__(self):
        A0 = open("meansr2.txt","rb") # 
        A1 = A0.readlines()
        A = np.zeros((len(A1),3))
        for i, l in enumerate(A1):
            li = l.split()
            A[i,0] = float(li[0])
            A[i,1] = float(li[1])
            A[i,2] = float(li[2])
        self.Lon = A[:,0]
        self.Lat = A[:,1]
        self.Z = A[:,2]
data = d()
numcols, numrows = 300, 300
xi = np.linspace(data.Lon.min(), data.Lon.max(), numrows)
yi = np.linspace(data.Lat.min(), data.Lat.max(), numcols)
xi, yi = np.meshgrid(xi, yi)

x, y, z = data.Lon, data.Lat, data.Z
points = np.vstack((x,y)).T
values = z
wanted = (xi, yi)
zi = griddata(points, values, wanted)

Defining map object

m = Basemap(projection = 'merc',llcrnrlon = 21, llcrnrlat = -18, urcrnrlon = 34, urcrnrlat = -8, resolution='h')
m.drawcountries(linewidth=0.5, linestyle='solid', color='k', antialiased=1, ax=None, zorder=None)
m.drawmapboundary(fill_color = 'white')
m.fillcontinents(color='coral',lake_color='blue')
parallels = np.arange(-18, -8, 2.)
m.drawparallels(parallels, color = 'black', linewidth = 0.5, labels=[True,False,False,False])
meridians = np.arange(22,34, 2.)
m.drawmeridians(meridians, color = '0.25', linewidth = 0.5, labels=[False,False,False,True])

import pylab as plt

an attempt to transform form lat/lon to map coordinates #lat = list(data.Lat) #lon = list(data.Lon) #x, y = m(lon,lat) comment: contourf is tried with (x, y, zi), then all the above definitions are rewritten with xi, # yi, including many different attempts to redefine x,y and lon, lat.J

The plot functions

fig = plt.figure(0, figsize=(8,4.5))
im = plt.contourf(xi, yi, zi)
plt.scatter(data.Lon, data.Lat, c= data.Z)
plt.colorbar()
plt.show()

The above give two plots side by side. Here is some data in case there is need to test

Lon   Lat     Z   Z2  pos
32.6  -13.6   41   9  CHIP
27.1  -16.9   43  12  CHOM
32.7  -10.2   46  14  ISOK
24.2  -13.6   33  13  KABO
28.5  -14.4   43  11  KABW
28.1  -12.6   33  16  KAFI
27.9  -15.8   46  13  KAFU
24.8  -14.8   44   9  KAOM
31.1  -10.2   35  14  KASA
25.9  -13.5   24   8  KASE
29.1   -9.8   10  13  KAWA
25.8  -17.8   39  11  LIVI
33.2  -12.3   44   8  LUND
28.3  -15.4   46  12  LUSA
27.6  -16.1   47   9  MAGO
28.9  -11.1   31  15  MANS
31.3   -8.9   39   9  MBAL
31.9  -13.3   45   9  MFUW
23.1  -15.3   31   9  MONG
31.4  -11.9   39   9  MPIK
27.1  -15.0   42  12  MUMB
24.4  -11.8   15   9  MWIN
28.6  -13.0   39   9  NDOL
31.3  -14.3   44  12  PETA
23.3  -16.1   39   5  SENA
30.2  -13.2   38  11  SERE
24.3  -17.5   32  10  SESH
26.4  -12.2   23  12  SOLW
23.1  -13.5   27  14  ZAMB

Any assistance will be appreciated

I would like to thank all those who have looked at my problem and may have tried to work on it. By consistent trying, it has come to my attention that the overlaying of the basemap on the contours actually works with the following lines After the map object definition

m = Basemap(projection = 'merc',llcrnrlon = 21, llcrnrlat = -18, urcrnrlon = 34, urcrnrlat = -8, resolution='h')

I have

x, y = m(xi, yi)
fig=plt.figure(figsize=(8,4.5))
cs = m.contour(x,y,zi,colors='b',linewidths=1.)

Contour(x,y,zi) plots the contours on the map. Since I was using contourf, I still have to find out why contourf does not give me the filled contours. Thank you very much all for the patience and tolerance.

来源:https://stackoverflow.com/questions/21061710/overlaying-a-basemap-on-contours

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