2017-09-22 24 views
0

車が動いているときにバックグラウンドで地図を表示したい。私はmatplotlibアニメーション機能を使用しています。動きはうまく見えます。しかし、私はマップをロードしている間、次のことを試しました。地図はロードされていません。ブラックパッチのみが表示されます。私はzorderも指定しようとしました。しかし、何も動作しません。matplotlibアニメーションでバックグラウンドで地図を表示

ani = animation.FuncAnimation(fig, animate, len(x11),interval=150, 
          blit=True, init_func=init, repeat=False) 

img = cbook.get_sample_data('..\\maps.png') 
image = plt.imread(img) 
plt.imshow(image) 
plt.show() 

答えて

1

あなたはscipy.misc import imreadで背景画像を読み込み、アニメーションの背景に描画するためにplt.imshowを使用することができます。

以下の例では、円を生成します(私たちはあなたの車を想定します)。背景に「usa_map.jpg」を置き、円の上を移動します。

ボーナス、あなたは、このようなanim.save('the_movie.mp4', writer = 'ffmpeg', fps=30)

ソースコードコードの上

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.image as mpimg 
import matplotlib.animation as animation 
from scipy.misc import imread 


img = imread("usa_map.jpg") 

fig = plt.figure() 
fig.set_dpi(100) 
fig.set_size_inches(7, 6.5) 

ax = plt.axes(xlim=(0, 20), ylim=(0, 20)) 
patch = plt.Circle((5, -5), 0.75, fc='y') 


def init(): 
    patch.center = (20, 20) 
    ax.add_patch(patch) 
    return patch, 

def animate(i): 
    x, y = patch.center 
    x = 10 + 3 * np.sin(np.radians(i)) 
    y = 10 + 3 * np.cos(np.radians(i)) 
    patch.center = (x, y) 
    return patch, 

anim = animation.FuncAnimation(fig, animate, 
           init_func=init, 
           frames=360, 
           interval=20, 
           blit=True) 

plt.imshow(img,zorder=0, extent=[0.1, 20.0, 0.1, 20.0]) 
anim.save('the_movie.mp4', writer = 'ffmpeg', fps=30) 
plt.show() 

は、米国を中心に動い円でanimatonを生成します使用してmp4形式のムービーとffmpegとしてエンコーダを使用してアニメーションを保存することができます地図。それはまた 'the_movie.mp4'として保存されます。私はここでアップロードできません。

結果画像
enter image description here

+0

感謝。それは完全に動作します – narasimman

+0

それが問題を解決したことを知ってうれしい。投票も高く評価されています。 –

関連する問題