Cartopy(或者是Matplotlib?)错误地从0,0

时间:2019-03-25 16:38:26

标签: python matplotlib cartopy

我正在尝试绘制IP地址的位置,因为它们使用Cartopy + Matplotlib连接到我们的一台服务器。我有一个CSV文件,其中包含我正在遍历的时间戳,IP,经纬度和经度,并为每经过的分钟创建一个新地图。

我一直在使用https://medium.com/udacity/creating-map-animations-with-python-97e24040f17b作为粗略指南。

我可以每分钟生成一个单独的帧,即在该分钟内连接多个IP,然后对其进行绘制。他们只是没有在正确的地方绘制...

def make_ips_map(date, data, ax=None, resolution='low'):
    if ax is None:
        fig = plt.figure(figsize=(19.2, 10.8))
        ax = plt.axes(projection=ccrs.Mercator(min_latitude=-65,
                                               max_latitude=70))

    ax.background_img(name='BM', resolution=resolution)
    ax.set_extent([-170, 179, -65, 70], crs=ccrs.PlateCarree())
    players = data[data['Timestamp'] < date]

    for ip, ip_data in players.groupby('IPaddress'):
        player_counts = ip_data.groupby(['long', 'lat']).count()

        # Get lists for longs and lats of each player
        index = list(player_counts.index)
        longs = [each[0] for each in index]

        lats = [each[1] for each in index]
        sizes = player_counts['IPaddress'] * 10

        ax.scatter(longs, lats, s=sizes,
                   color='blue', alpha=0.8,
                   transform=ccrs.PlateCarree())

...

df = pd.read_csv('test_lat_long.csv', names=['Timestamp', 'IPaddress', 'lat', 'long'])
df['Timestamp'] = pd.to_datetime(df['Timestamp'], errors='coerce')

start_date = datetime(2019, 3, 20, 18, 00, 00)
end_date = datetime(2019, 3, 20, 20, 38, 0)
fig = plt.figure(figsize=(19.2, 10.8))
ax = plt.axes(projection=ccrs.Mercator(min_latitude=-65, max_latitude=70))

# Generate an image for each minute between start_date and end_date
total_minutes = range((((end_date - start_date).seconds)//60)%60)

for ii, minutes in enumerate(total_minutes):
    date = start_date + timedelta(minutes=minutes)
    make_ips_map(date, df, ax=ax, resolution='low')
    fig.tight_layout(pad=-0.5)
    fig.savefig(f"frames/frame_{ii:04d}.png", dpi=100,
                frameon=False, facecolor='black')
    ax.clear()

这些点以从0,0开始的曲线绘制,该曲线向上并向右延伸。

我怀疑这与ax.scatter使用的投影(墨卡托(Mercator))和变换(PlateCarree)之间不匹配有关,但无法弄清楚。

1 个答案:

答案 0 :(得分:0)

通常我在SO上发布问题时,大约30分钟后,我便找到了解决方案。

longs = [each[0] for each in index]

lats = [each[1] for each in index]

...正在返回字符串。将它们抛到浮标上后,一切都开始完美运行。