-2

In this Kaggle Kernel https://www.kaggle.com/asindico/new-york-taxi-exploration I can't manage to show a matplotlib animation using on Basemap and hexbin.

Here is the code snippet

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)
Andrea Sindico
  • 7,358
  • 6
  • 47
  • 84
  • why the downvote? – Andrea Sindico Jul 22 '17 at 09:09
  • I did not (yet) downvote but it's clear why someone else did: Questions that sound like "Here is my code, please find the problem" are not very well received. You might stick to [ask] and provide a clear problem description. – ImportanceOfBeingErnest Jul 22 '17 at 09:14
  • Now I did downvote, because after spending 10 minutes of getting the data off the kaggle website and running the code, it turns out the code is running fine as expected. Wasting 15 minutes of other peoples time by not providing a proper problem description would actually deserve more than a downvote. – ImportanceOfBeingErnest Jul 22 '17 at 09:32
  • Did you manage to show the animated map? How ? as you can see in the published kernel the map is not animated. You do not need to download the Kaggle Kernel. You can fork it. – Andrea Sindico Jul 22 '17 at 10:20
  • The animation is saved into `pickup_animation.gif`. You may use whatever tool you like to show it. My point is that you need to clearly state what you expect, what you get and in how far the two differ in order to have someone help you. – ImportanceOfBeingErnest Jul 22 '17 at 10:31
  • The animation is even shown in the linked kaggle on the ["Output" tab](https://www.kaggle.com/asindico/new-york-taxi-exploration/output). – ImportanceOfBeingErnest Jul 22 '17 at 10:37
  • How can I show it in the notebook? – Andrea Sindico Jul 22 '17 at 10:58

1 Answers1

0

To display the animated gif inside the notebook you may use

from IPython.display import Image, display
display(Image(url='pickup_animation.gif'))

You may also show the animation as html5 video

from IPython.display import HTML
HTML(anim.to_html5_video())

You may also show the matplotlib animation directly, using the notebook backend instead of the inline backend

%matplotlib notebook
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712