我一直在使用matplotlib和底图来显示有关纽约市的一些信息.到目前为止,我一直在关注this guide,但遇到了一个问题.我试图在可视化中显示曼哈顿岛,但我不知道为什么底图没有将其显示为岛.
这是底图为我提供的可视化效果:
这是我正在使用的边界框的屏幕截图:
这是生成图像的代码:
wl = -74.04006sl = 40.683092el = -73.834067nl = 40.88378m = Basemap(resolution='f',# c,l,i,h,f or None projection='merc',area_thresh=50,lat_0=(wl + sl)/2,lon_0=(el + nl)/2,llcrnrlon= wl,llcrnrlat= sl,urcrnrlon= el,urcrnrlat= nl)m.drawmapboundary(fill_color='#46bcec')m.fillcontinents(color='#f2f2f2',lake_color='#46bcec')m.drawcoastlines()m.drawrivers()
我以为它可能会考虑河流之间的水,但是m.drawrivers()似乎没有解决问题.任何帮助显然都非常感激.
提前致谢!
最佳答案一种为您的地块获取质量更好的基础地图的方法是,以适当的缩放级别从Web地图图块构建一个.在这里,我演示了如何从openstreetmap Web地图服务器获取它们.在这种情况下,我将缩放级别设置为10,并获得2个地图图块,以将其合并为单个图像数组.缺点之一是组合图像的范围始终大于我们要求的值.这是工作代码:from mpl_toolkits.basemap import Basemapimport matplotlib.pyplot as pltimport numpy as npimport mathimport urllib2import StringIOfrom PIL import Image# === Begin block1 ===# Credit: BerndGit,answered Feb 15 '15 at 19:47. And ...# Source: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenamesdef deg2num(lat_deg,lon_deg,zoom): '''Lon./lat. to tile numbers''' lat_rad = math.radians(lat_deg) n = 2.0 ** zoom xtile = int((lon_deg + 180.0) / 360.0 * n) ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n) return (xtile,ytile)def num2deg(xtile,ytile,zoom): '''Tile numbers to lon./lat.''' n = 2.0 ** zoom lon_deg = xtile / n * 360.0 - 180.0 lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n))) lat_deg = math.degrees(lat_rad) return (lat_deg,lon_deg) # NW-corner of the tile. def getimageCluster(lat_deg,delta_lat,delta_long,zoom): # access map tiles from internet # no access/key or password is needed smurl = r"http://a.tile.openstreetmap.org/{0}/{1}/{2}.png" # useful snippet: smurl.format(zoom,xtile,ytile) -> complete URL # x increases L-R; y top-Bottom xmin,ymax =deg2num(lat_deg,zoom) # get tile numbers (x,y) xmax,ymin =deg2num(lat_deg+delta_lat,lon_deg+delta_long,zoom) # PIL is used to build new image from tiles Cluster = Image.new('RGB',((xmax-xmin+1)*256-1,(ymax-ymin+1)*256-1) ) for xtile in range(xmin,xmax+1): for ytile in range(ymin,ymax+1): try: imgurl = smurl.format(zoom,ytile) print("opening: " + imgurl) imgstr = urllib2.urlopen(imgurl).read() # Todo: study,what these do? tile = Image.open(StringIO.StringIO(imgstr)) Cluster.paste(tile,Box=((xtile-xmin)*256,(ytile-ymin)*255)) except: print("Couldn't download image") tile = None return Cluster# ===End Block1===# Credit to myselfdef getextents(latmin_deg,lonmin_deg,zoom): '''Return LL and UR,each with (long,lat) of real extent of combined tiles. latmin_deg: bottom lat of extent lonmin_deg: left long of extent delta_lat: extent of lat delta_long: extent of long,all in degrees ''' # Tile numbers(x,y): x increases L-R; y top-Bottom xtile_LL,ytile_LL = deg2num(latmin_deg,zoom) #get tile numbers as specifIEd by (x,y) xtile_UR,ytile_UR = deg2num(latmin_deg + delta_lat,lonmin_deg + delta_long,zoom) # from tile numbers,we get NW corners lat_NW_LL,lon_NW_LL = num2deg(xtile_LL,ytile_LL,zoom) lat_NW_LLL,lon_NW_LLL = num2deg(xtile_LL,ytile_LL+1,zoom) # next down below lat_NW_UR,lon_NW_UR = num2deg(xtile_UR,ytile_UR,zoom) lat_NW_URR,lon_NW_URR = num2deg(xtile_UR+1,zoom) # next to the right # get extents minLat = lat_NW_LLL minLon = lon_NW_LL maxLat = lat_NW_UR maxLon = lon_NW_URR return (minLon,maxLon,minLat,maxLat) # (left,right,bottom,top) in degrees# OP's values of extents for target area to plot# some changes here (with larger zoom level) may lead to better final plotwl = -74.04006sl = 40.683092el = -73.834067nl = 40.88378lat_deg = sllon_deg = wld_lat = nl - sld_long = el - wlzoom = 10 # zoom level# Acquire images. The combined images will be slightly larger that the extentstimg = getimageCluster(lat_deg,d_lat,d_long,zoom)# This computes real extents of the combined tile images,and get (left,top)latmin_deg,delta_long = sl,wl,nl-sl,el-wl(left,top) = getextents(latmin_deg,zoom) #units: degrees# Set Basemap with proper parametersm = Basemap(resolution='h',# h is nice projection='merc',lat_0=(bottom + top)/2,lon_0=(left + right)/2,llcrnrlon=left,llcrnrlat=bottom,urcrnrlon=right,urcrnrlat=top)fig = plt.figure()fig.set_size_inches(10,12)m.imshow(np.asarray(timg),extent=[left,top],origin='upper' )m.drawcoastlines(color='gray',linewidth=3.0) # intentionally thick line#m.fillcontinents(color='#f2f2f2',lake_color='#46bcec',Alpha=0.6)plt.show()
希望能帮助到你.结果图:
编辑
裁剪图像以获得确切的绘图区域并不困难. PIL模块可以处理. Numpy的数组切片也可以.
总结以上是内存溢出为你收集整理的python-为什么Matplotlib底图不显示岛? 全部内容,希望文章能够帮你解决python-为什么Matplotlib底图不显示岛? 所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)