【问题标题】:Plotting animation on basemap在底图上绘制动画
【发布时间】:2018-08-29 16:45:42
【问题描述】:

我正在尝试为在我的地图上移动的情节设置动画(飞机的关注领域)。我需要它来遍历我的坐标,所以我尝试使用 for 循环。当我运行它时,我得到了两个帧,第一个是空的,第二个只是一个空地图。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.basemap import Basemap

#map of Puerto Rico
map=Basemap(projection='merc', llcrnrlat=17.5,urcrnrlat=19.,llcrnrlon=-67.5, urcrnrlon=-65, epsg=4139)
map.arcgisimage(service='World_Shaded_Relief', xpixels = 2000)
plt.title("Flight Path of sbet 0059")

#sample coordinates
lon=[-63,-64,-65,-66]
lon=[int(l) for l in lon]
lat=[17., 17.5, 18., 18.5]
lat=[int(l) for l in lat]
time=[1, 3, 5, 7]
time=[int(l) for l in time]

fig=plt.figure()



for a1,b1 in zip(lon,lat):

    def init():
        fig
        return(fig),

    def animate(i):
        x,y=map(a1,b1)
        map.plot(x,y, linewidth = 1,color = 'm')
        return(map),

    anim=animation.FuncAnimation(fig, animate, frames=len(time), interval=1000)

    plt.show()

有谁知道问题是什么以及我如何让情节在地图上移动?谢谢!

【问题讨论】:

    标签: python for-loop animation matplotlib-basemap


    【解决方案1】:

    您的代码有很多问题。也许我只是在这里列出它们,然后如果您有不明白的地方,您可以提出问题:

    1. 如果尚未打开图窗,则调用Basemap 将生成一个新图窗。这使得对plt.figure()(有点)的额外调用变得多余。我通常会打电话给plt.figure,但之前打电话给Basemap
    2. 当您使用FuncAnimation 时,您不必循环遍历要设置动画的值——FuncAnimation 正在为您执行此操作。您唯一需要提供的是根据FuncAnimation 将提供给该函数的整数参数更新当前绘图的函数。换句话说,您编写的整个 for 循环不是要走的路。
    3. 只调用一次plt.show()。现在,您每次运行 for 循环时都会调用它。
    4. 不要在每次想要更新绘图时调用plot 命令。如果您的代码中的其他所有内容都是正确的,那么您将得到很多行,而不仅仅是一行。而是更新plot 返回的线对象的数据。请参阅下面的代码,这是如何完成的。
    5. 您提供的数据点数量与要制作动画的帧数量不匹配(4 vs 1000),这意味着动画将长时间“空”运行。
    6. 您提供的大部分坐标都在您显示的地图区域之外。
    7. 请勿调用 Basemap 对象map,即使他们在教程中这样做了。 map 是 python 中的保留字。而是使用mbmap 之类的。

    下面我试图纠正你的代码的问题,但它可能会因你的需要而改变太多,所以请询问是否有任何不清楚的地方:

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    from mpl_toolkits.basemap import Basemap
    
    fig=plt.figure()
    
    #map of Puerto Rico
    bmap=Basemap(projection='merc', llcrnrlat=17.5,urcrnrlat=19.,llcrnrlon=-67.5, urcrnrlon=-65, epsg=4139)
    bmap.arcgisimage(service='World_Shaded_Relief', xpixels = 2000)
    plt.title("Flight Path of sbet 0059")
    
    #sample coordinates
    ##lon=[-63,-64,-65,-66]
    ##lon=[int(l) for l in lon]
    ##lat=[17., 17.5, 18., 18.5]
    ##lat=[int(l) for l in lat]
    ##time=[1, 3, 5, 7]
    ##time=[int(l) for l in time]
    
    #generate the flight coordinates (just a straight line with 100 points)
    N = 100
    lon = np.linspace(-64, -68, N)
    lat = np.linspace(17, 19.5, N)
    
    #only convert the coordinates once    
    x,y = bmap(lon, lat)
    
    #generate the original line object with only one point
    line, = bmap.plot(x[0], y[0], linewidth = 1, color = 'm')
    
    def animate(i):
        #this is doing all the magic. Every time animate is called, the line
        #will become longer by one point:
        line.set_data(x[:i],y[:i])  
        return line
    
    anim=animation.FuncAnimation(fig, animate, frames=N, interval=N)
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      • 2018-01-14
      • 2012-08-28
      • 2020-11-06
      • 2020-10-12
      相关资源
      最近更新 更多