如何设置matplotlib的drawgreatcircle函数的动画? [英] How to animate matplotlib's drawgreatcircle function?

查看:199
本文介绍了如何设置matplotlib的drawgreatcircle函数的动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小程序,使用NHL城市,然后绘制球队整个赛季的路线.

I have created a small program which takes an NHL city and then draws the path the team travels throughout their season.

生成的图形混乱:

所以我想到,如果我对飞行路线进行动画处理,就像看一部印第安纳·琼斯电影一样,那条线从一个点延伸到另一个,那将很有趣.

So I got the idea that it would be interesting if I animated the flight paths, sort of like watching an Indiana Jones movie, where the line grows from one point to another.

通过查看其他matplotlib示例,我的理解是animation函数接受一个函数,计算其输出,然后更新图形.我看不到drawgreatcircle怎么可能,因为每当我叫它时,我都会得到完整的一行.

My understanding from looking at other matplotlib samples is that the animation function takes in a function, calculates it's output, and then updates the graphic. I don't see how this would be possible with drawgreatcircle since whenever I call it I am given a completed line.

关于如何解决这个问题的任何想法吗?

Any idea on how I can approach this?

这是下面示例代码中的示例图像

Here's a sample image from the sample code below

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

fig = plt.figure(figsize=(10, 10))
m = Basemap(projection='merc', resolution=None,
                    llcrnrlon=-125, llcrnrlat=25, # LL = lower left
                    urcrnrlon=-60, urcrnrlat=55) #UR = upper right 
m.etopo(scale=0.5, alpha=0.5)


# Ottawa to Anaheim
# Ottawa
lat1 = 45.4215
lon1 = -75.6972

# Anaheim
lat2 = 33.8353
lon2 = -117.9145

m.drawgreatcircle(lon1,lat1,lon2,lat2)

推荐答案

drawgreatcicle返回matplotlib line2D,可以使用get_data从中获取数据.因此,想法是画一个大圆圈,获取数据,然后删除它.使用这些数据,可以执行动画,遍历整个数组.

drawgreatcicle returns matplotlib line2D from which one can obtain the data using get_data. So the idea would be to draw the great circle, get the data and afterwards delete it. Using the data, one can perform an animation, iterating over the array.

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

fig = plt.figure(figsize=(6, 4))
m = Basemap(projection='merc', resolution=None,
                    llcrnrlon=-125, llcrnrlat=25, # LL = lower left
                    urcrnrlon=-60, urcrnrlat=55) #UR = upper right 
m.etopo(scale=0.5, alpha=0.5)

# Ottawa to Anaheim
# Ottawa
lat1 = 45.4215
lon1 = -75.6972

# Anaheim
lat2 = 33.8353
lon2 = -117.9145

line, = m.drawgreatcircle(lon1,lat1,lon2,lat2)
x,y = line.get_data()

line.remove()
del line

line, = plt.plot([],[])

def update(i):
    line.set_data(x[:i], y[:i])

ani = matplotlib.animation.FuncAnimation(fig, update, frames=len(x), interval=100)
ani.save(__file__+".gif", writer="imagemagick", fps=10)
plt.tight_layout()
plt.show()

这篇关于如何设置matplotlib的drawgreatcircle函数的动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆