2017-03-09 16 views
0

私はCartopyを使用して簡単なアニメーションを作成しようとしています。基本的に地図に数行を描くだけです。これまでのところ、私は以下を試しています:Cartopyの簡単なアニメーションの操作

import matplotlib.pyplot as plt 
import cartopy.crs as ccrs 
import matplotlib.animation as animation 
import numpy as np 

ax = plt.axes(projection=ccrs.Robinson()) 
ax.set_global() 
ax.coastlines() 

lons = 10 * np.arange(1, 10) 
lats = 10 * np.arange(1, 10) 

def animate(i): 

    plt.plot([lons[i-1], lons[i]], [lats[i-1], lats[i]], color='blue', transform=ccrs.PlateCarree()) 
    return plt 

anim = animation.FuncAnimation(plt.gcf(), animate, frames=np.arange(1, 8), init_func=None, interval=2000, blit=True) 

plt.show() 

これはなぜ動作しないのですか?

答えて

1

これはcartopyとは無関係ですが、私は推測します。問題は、アニメーション関数からpyplotを返すことができないことです。 (それはあなたが本屋を読み取ることができませんなぜ本を買うの、あなたが全体のブックストアを購入して、疑問に思うだろう代わりのようなものです。)

最も簡単な解決策はオフ、ブリット有効にすることです:

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
import numpy as np 

fig, ax = plt.subplots() 

lons = 10 * np.arange(1, 10) 
lats = 10 * np.arange(1, 10) 

def animate(i): 
    plt.plot([lons[i-1], lons[i]], [lats[i-1], lats[i]], color='blue') 

anim = animation.FuncAnimation(plt.gcf(), animate, frames=np.arange(1, 8), 
           init_func=None, interval=200, blit=False) 

plt.show() 

何らかの理由でblittingが必要な場合(アニメーションが遅すぎたり、CPUが多すぎる場合)、描画するLine2Dオブジェクトのリストを返す必要があります。

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
import numpy as np 

fig, ax = plt.subplots() 

lons = 10 * np.arange(1, 10) 
lats = 10 * np.arange(1, 10) 
lines = [] 

def animate(i): 
    line, = plt.plot([lons[i-1], lons[i]], [lats[i-1], lats[i]]) 
    lines.append(line) 
    return lines 

anim = animation.FuncAnimation(plt.gcf(), animate, frames=np.arange(1, 8), 
           interval=200, blit=True, repeat=False) 

plt.xlim(0,100) #<- remove when using cartopy 
plt.ylim(0,100) 
plt.show() 
関連する問題