微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python通过matplotlib和cartopy绘制了一些点而忽略了其他点,为什么呢?

如何解决Python通过matplotlib和cartopy绘制了一些点而忽略了其他点,为什么呢?

在使用matplotlib 3.3.2和cartopy 0.18.0的Python 3.8.5中,我使用以下代码在地球上绘制了点的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,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],[10])

第二个DataFrame y可以在这里下载:https://gofile.io/d/oyCL1r获得的两个图片

enter image description here

enter image description here

如您所见,第一个图片是正确的,但是第二个图片仅绘制了一个点,即使我的文件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]

如何确定所有点都已绘制?另外,地图是可缩放的,有没有办法保存它并保留此功能

解决方法

对于第二个情节,我编写了一个简短的脚本并运行它并得到了要点。

from io import StringIO

# data for check plot
str = """ID  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
7  26.361809 -80.04008
8  23.095477 -80.00000
9  23.422111 -80.00000
10  23.487437 -80.00000
11  25.839196 -80.00000"""

df = pd.read_csv(StringIO(str),sep='\s+',index_col='ID')
#df  #OK

# plot the data
plotPt([df],['red'],[1])

so_plot

要查看地图上的缩放和互动功能,请寻找folium

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。