在这个Kaggle Kernel https://www.kaggle.com/asindico/new-york-taxi-exploration中,我无法使用Basemap和hexbin显示matplotlib动画。
以下是代码段
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib import cm
from matplotlib import animation, rc
from IPython.display import HTML
%matplotlib inline
#40.758896, -73.985130.
west, south, east, north = -74.1, 40.60, -73.80, 40.9
day=1
df_day=df[((df.pickup_datetime<'2016-01-'+str(day+1))&
(df.pickup_datetime>='2016-01-'+str(day)))]
fig,axarr = plt.subplots()
m = Basemap(projection='merc', llcrnrlat=south, urcrnrlat=north,
llcrnrlon=west, urcrnrlon=east, lat_ts=south, resolution='i',ax=axarr)
def init():
df_time= df_day[((df_day.pu_hour>=0)&(df_day.pu_hour<0))]
x, y = m(df_time['pickup_longitude'].values, df_time['pickup_latitude'].values)
m.hexbin(x, y, gridsize=300, bins='log', cmap=cm.YlOrRd_r);
return (m,)
def animate(i):
df_time= df_day[((df_day.pu_hour>=i)&(df_day.pu_hour<i+1))]
x, y = m(df_time['pickup_longitude'].values, df_time['pickup_latitude'].values)
m.hexbin(x, y, gridsize=300, bins='log', cmap=cm.YlOrRd_r);
return (m,)
anim = animation.FuncAnimation(fig, animate, init_func=init,frames=5, interval=1,blit=False,repeat=True)
fig.set_size_inches(12,10)
anim.save('pickup_animation.gif', writer='imagemagick', fps=2)
答案 0 :(得分:0)
要在笔记本中显示动画gif,您可以使用
from IPython.display import Image, display
display(Image(url='pickup_animation.gif'))
您也可以将动画显示为html5视频
from IPython.display import HTML
HTML(anim.to_html5_video())
您也可以使用笔记本后端而不是内联后端直接显示matplotlib动画
%matplotlib notebook