自动将matplotlib basemap居中

2024-09-30 16:29:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要一个解决方案,以自动居中的基础地图上我的坐标数据。在

我有一些东西要自动居中,但结果面积比我的数据实际使用的面积大得多。我希望绘图以绘图坐标为边界,而不是从纬度/经度边界绘制的区域。在

我用John Cook's code来计算(假设完美)球体上两点之间的距离。在

首次尝试

这是我开始写的剧本。这导致宽度和高度对于数据区域来说太小了,而中心纬度(lat0)太南。在

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import sys
import csv
import spheredistance as sd


print '\n'
if len(sys.argv) < 3:
    print >>sys.stderr,'Usage:',sys.argv[0],'<datafile> <#rows to skip>'
    sys.exit(1)
print '\n'

dataFile = sys.argv[1]
dataStream = open(dataFile, 'rb')
dataReader = csv.reader(dataStream,delimiter='\t')
numRows = sys.argv[2]

dataValues = []
dataLat = []
dataLon = []

print 'Plotting Data From: '+dataFile

dataReader.next()
for row in dataReader:
    dataValues.append(row[0])
    dataLat.append(float(row[1]))
    dataLon.append(float(row[2]))

# center and set extent of map
earthRadius = 6378100 #meters
factor = 1.00

lat0new = ((max(dataLat)-min(dataLat))/2)+min(dataLat)
lon0new = ((max(dataLon)-min(dataLon))/2)+min(dataLon)

mapH = sd.distance_on_unit_sphere(max(dataLat),lon0new,
            min(dataLat),lon0new)*earthRadius*factor

mapW = sd.distance_on_unit_sphere(lat0new,max(dataLon),
            lat0new,min(dataLon))*earthRadius*factor

# setup stereographic basemap.
# lat_ts is latitude of true scale.
# lon_0,lat_0 is central point.
m = Basemap(width=mapW,height=mapH,
            resolution='l',projection='stere',\
            lat_0=lat0new,lon_0=lon0new)

#m.shadedrelief()
m.drawcoastlines(linewidth=0.2)
m.fillcontinents(color='white', lake_color='aqua')

#plot data points (omitted due to ownership)
#x, y = m(dataLon,dataLat)
#m.scatter(x,y,2,marker='o',color='k')

# draw parallels and meridians.
m.drawparallels(np.arange(-80.,81.,20.), labels=[1,0,0,0], fontsize=10)
m.drawmeridians(np.arange(-180.,181.,20.), labels=[0,0,0,1], fontsize=10)
m.drawmapboundary(fill_color='aqua')

plt.title("Example")
plt.show()

enter image description here


Tags: 数据importassyspltminmaxcolor
1条回答
网友
1楼 · 发布于 2024-09-30 16:29:30

在生成了一些随机数据之后,很明显我选择的边界不适用于这个投影(红线)。使用map.drawgreatcircle地图(),我首先看到了我想要的边界,同时放大了随机数据的投影。在

Red lines are old calculated widths and height

我用最南纬的纵向差(蓝色水平线)校正经度。在

我用毕达哥拉斯定理来求解垂直距离,确定了纬度范围,知道了最北部的纵向边界和最南端的中心点(蓝色三角形)之间的距离。在

def centerMap(lats,lons,scale):
    #Assumes -90 < Lat < 90 and -180 < Lon < 180, and
    # latitude and logitude are in decimal degrees
    earthRadius = 6378100.0 #earth's radius in meters

    northLat = max(lats)
    southLat = min(lats)
    westLon = max(lons)
    eastLon = min(lons)

    # average between max and min longitude 
    lon0 = ((westLon-eastLon)/2.0)+eastLon

    # a = the height of the map
    b = sd.spheredist(northLat,westLon,northLat,eastLon)*earthRadius/2
    c = sd.spheredist(northLat,westLon,southLat,lon0)*earthRadius

    # use pythagorean theorom to determine height of plot
    mapH = pow(pow(c,2)-pow(b,2),1./2)
    arcCenter = (mapH/2)/earthRadius

    lat0 = sd.secondlat(southLat,arcCenter)

    # distance between max E and W longitude at most souther latitude
    mapW = sd.spheredist(southLat,westLon,southLat,eastLon)*earthRadius

    return lat0,lon0,mapW*scale,mapH*scale

lat0center,lon0center,mapWidth,mapHeight = centerMap(dataLat,dataLon,1.1)

因此,在这个例子中,纬度0(或纬度中心)是这个三角形高度的一半,我用John Cooks方法解决了这个问题,但是为了解决一个未知的坐标,同时知道第一个坐标(南部边界的中经度)和弧长(总高度的一半)。在

^{pr2}$

更新: 使用pyprojGeod类方法geod.fwd()geod.inv()可以获得更高精度的上述函数以及两个坐标之间的距离。我在Erik Westra的Python for Geospatial Development中发现了这一点,这是一个很好的资源。在

更新: 我现在已经验证了这同样适用于Lambert共形圆锥(lcc)投影。在

相关问题 更多 >