【发布时间】:2021-02-19 22:36:08
【问题描述】:
在 Python 3.8.5 中使用 matplotlib 3.3.2 和 cartopy 0.18.0,我使用以下代码在地球上绘制了一个点的 DataFrame:
import matplotlib.pyplot as plt
import cartopy
import cartopy.io.img_tiles
import numpy as np
import pandas as pd
def plotPt(xx,clr=['red'],msize=[1.0],name=''): #plots the geo points on a globe
terrain=cartopy.io.img_tiles.Stamen('terrain-background') #Create a Stamen terrain background instance.
fig = plt.figure(figsize=(10,10))
a=fig.add_subplot(1,1,1,projection=cartopy.crs.EqualEarth()) #Create a GeoAxes in the tile's projection.
#projection: terrain.crs or cartopy.crs. with Orthographic(),Mollweide(),Robinson(),EqualEarth(),PlateCarree()
x=pd.concat(xx); rng= [x['LON'].min(),x['LON'].max(),x['LAT'].min(),x['LAT'].max()]; del x #range of the map
a.set_extent(rng,crs=cartopy.crs.Geodetic()) #Limit the extent of the map to a small longitude/latitude range.
a.add_image(terrain,5) #Add the Stamen data at specified zoom level.
for i in range(len(xx)):
a.plot(xx[i]['LON'],xx[i]['LAT'], color=clr[i], linewidth=0, marker='o', markersize=msize[i], alpha=0.99, transform=cartopy.crs.Geodetic())
plt.subplots_adjust(left=0,right=1,bottom=0,top=1,wspace=0,hspace=0); plt.show()
#fig.savefig('/home/leon/plotPt'+name+'.png',bbox_inches='tight')
n=100; x=pd.DataFrame([(lat,lon) for lat in np.linspace(20,30,n) for lon in np.linspace(-100,-80,n)],columns=['LAT','LON']); plotPt([x],['red'],[1])
y=pd.read_csv('/home/leon/points.csv')[['LAT','LON']]; print(y); plotPt([y],['red'],[10])
第二个DataFramey可以在这里下载:https://gofile.io/d/oyCL1r得到的两张图片是
如您所见,第一张图是正确的,但第二张图只绘制了一个点,即使我的文件 points.csv 的内容是:
LAT LON
0 24.989950 -97.47495
1 24.989950 -97.43487
2 25.055276 -97.43487
3 24.989950 -97.39479
4 25.055276 -97.39479
... ... ...
6657 26.361809 -80.04008
6658 23.095477 -80.00000
6659 23.422111 -80.00000
6660 23.487437 -80.00000
6661 25.839196 -80.00000
[6662 rows x 2 columns]
如何确保绘制所有点?另外,地图是可缩放的,有没有办法保存它并保留这个功能?
【问题讨论】:
-
你能把你的 csv 文件贴在某个地方并链接到它吗?你可以把它发到pastebin.com
-
@DopplerShift 感谢您的回复。我已经上传了文件,并在开篇文章中提供了指向它的链接。您对为什么会出现问题有一些线索吗?
标签: python matplotlib plot geo cartopy