I am trying to learn matplotlib and the mapping function Basemap, and am trying to plot a simple list of lat/long coordinates on a map. However, the Basemap coordinate conversion to simple x/y coordinates are plotting points wildly off the map, and I can't work out why. My code is below:
我正在尝试学习matplotlib和映射函数Basemap,并尝试在地图上绘制一个简单的lat/long坐标列表。然而,Basemap坐标转换到简单的x/y坐标,却在地图上疯狂地画出了点,我不知道为什么。我的代码如下:
locationData = parseFile(locationFile)
fig = plt.figure(figsize=(8,8))
m = Basemap(projection='merc', resolution='h',
llcrnrlat=49, llcrnrlon=-13.5,
urcrnrlat=59.5, urcrnrlon=4)
m.drawcoastlines()
m.drawmapboundary(fill_color='aqua')
m.fillcontinents(color='green', lake_color='aqua')
index=0
for entry in locationData:
lat = entry["latitudeE7"]/1E7
long = entry["longitudeE7"]/1E7
x,y = m(lat, long)
print("Plotting {} {} to x{} y{}".format(lat,long,x,y))
plt.plot(x,y, 'ok', markersize=20)
# break at 30 points for testing
if index>30: break
index+=1
plt.show()
And here is the output I get from the print statement:
这是我从打印语句中得到的输出:
Plotting 52.------- 1.------- to x79364--.------- y-31476--.-------
Plotting 52.------- 1.------- to x79368--.------- y-31475--.-------
Plotting 52.------- 1.------- to x79362--.------- y-31471--.-------
Plotting 52.------- 1.------- to x79361--.------- y-31472--.-------
Plotting 52.------- 1.------- to x79360--.------- y-31475--.-------
Plotting 52.------- 1.------- to x79365--.------- y-31476--.-------
Plotting 52.------- 1.------- to x79361--.------- y-31476--.-------
...
I've censored the exact values for obvious reasons, but you can see that the intended values of 52°N 1°E which points to the UK are being placed wildly off chart, which is of the UK. Expanding the map to the whole globe reveals that it is plotting the points off the north coast of Madagascar.
我审查的确切值明显的原因,但是你可以看到52°N 1°E的目的价值指向英国被放置广图,这是英国的。把地图扩展到全球,就会发现地图上标出了马达加斯加北部海岸的点。
I am taking the coordinates from a Google Location History download, then dividing it by 10^7 as they are stored as integers. The print statement shows that these are being parsed correctly.
我下载从谷歌位置坐标的历史,然后除以10 ^ 7存储为整数。打印语句显示这些内容被正确解析。
I am new to matplotlib and basemap, and am running Python 3.6 on Windows 10. Any help?
我是matplotlib和basemap的新手,在Windows 10上运行Python 3.6。任何帮助吗?
edit - posted my old code my mistake
编辑-张贴我的旧代码我的错误
1 个解决方案
#1
2
From the basemap documentation
从技术文档
Calling a Basemap class instance with the arguments lon, lat will convert lon/lat (in degrees) to x/y map projection coordinates (in meters). If optional keyword inverse is True (default is False), the inverse transformation from x/y to lon/lat is performed.
使用参数lon调用Basemap类实例,lat将lon/lat(单位为度)转换为x/y映射投影坐标(单位为米)。如果可选关键字逆为真(默认为假),则执行从x/y到lon/lat的逆变换。
Instead of x,y = m(lat, long)
you hence need
因此需要y = m(lat, long)而不是x
x,y = m(long, lat)
#1
2
From the basemap documentation
从技术文档
Calling a Basemap class instance with the arguments lon, lat will convert lon/lat (in degrees) to x/y map projection coordinates (in meters). If optional keyword inverse is True (default is False), the inverse transformation from x/y to lon/lat is performed.
使用参数lon调用Basemap类实例,lat将lon/lat(单位为度)转换为x/y映射投影坐标(单位为米)。如果可选关键字逆为真(默认为假),则执行从x/y到lon/lat的逆变换。
Instead of x,y = m(lat, long)
you hence need
因此需要y = m(lat, long)而不是x
x,y = m(long, lat)