python-为什么Matplotlib底图不显示岛?

前端之家收集整理的这篇文章主要介绍了python-为什么Matplotlib底图不显示岛? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我一直在使用matplotlib和底图来显示有关纽约市的一些信息.到目前为止,我一直在关注this guide,但遇到了一个问题.我试图在可视化中显示曼哈顿岛,但我不知道为什么底图没有将其显示为岛.

这是底图为我提供的可视化效果

enter image description here

这是我正在使用的边界框的屏幕截图:

enter image description here

这是生成图像的代码

wl = -74.04006
sl = 40.683092
el = -73.834067
nl = 40.88378

m = 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 Basemap
import matplotlib.pyplot as plt
import numpy as np
import math
import urllib2
import StringIO
from PIL import Image

# === Begin block1 ===
# Credit: BerndGit,answered Feb 15 '15 at 19:47. And ...
# Source: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames

def 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 myself
def 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 plot
wl = -74.04006
sl = 40.683092
el = -73.834067
nl = 40.88378

lat_deg = sl
lon_deg = wl
d_lat = nl - sl
d_long = el - wl
zoom = 10   # zoom level

# Acquire images. The combined images will be slightly larger that the extents
timg = 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 parameters
m = 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()

希望能帮助到你.结果图:

enter image description here

编辑

裁剪图像以获得确切的绘图区域并不困难. PIL模块可以处理. Numpy的数组切片也可以.

原文链接:https://www.f2er.com/python/533047.html

猜你在找的Python相关文章