【问题标题】:Python via matplotlib and cartopy draws some points and ignores others, why?Python 通过 matplotlib 和 cartopy 绘制了一些点而忽略了其他点,为什么?
【发布时间】: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


【解决方案1】:

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

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])

要在地图上进行缩放和交互功能,请查找folium

【讨论】:

  • 如果我只绘制x 的一个子集,我会得到所有的点,但是当绘制整个x 时,图片中只有一个点。我可以在哪里上传我的 CSV 文件,以便您自己查看问题?
  • @Leo 检查你的数据,试试基本的 min max ave。
  • 我不明白你所说的 min/avg/max 是什么意思。如前所述,问题偶尔会发生,并非总是如此。一个例子是我的文件,我很想分享,只是不知道如何。
  • @Leo 在你的例子中,简单的数据图,x['LON'].plot()x['LAT'].plot() 可以提供一些关于数据的想法。但问题可能是其他原因。
  • 数据没有问题。我可以用另一种方法正常绘制它(使用plotly,但图片看起来不那么花哨)。这就是为什么我想让这个方法正常工作。
猜你喜欢
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 2018-04-26
  • 2022-06-26
  • 2020-03-11
相关资源
最近更新 更多