5  Spatial Informations

5.1 HeatMap

Code
def display_heatmap(heatmap_data):
    heatmap_data = heatmap_data.pivot(index='start_station_id', columns='end_station_id', values='trip_count')
    return px.imshow(heatmap_data, title="Heatmap of Start/End stations in 2018", labels=dict(x="End Station", y="Start Station", color="Number of Trips"))

display_heatmap(heatmap_data_2018)

5.2 Interactive map

Code
def display_interactive_map(df, bonus=""):
    df = df.sort_values("hour")
    fig = px.density_mapbox(df, lat='lat', lon='lng', z='count', radius=30,
                            center=dict(lat=40.7128, lon=-74.0060), zoom=10,
                            mapbox_style="carto-positron", animation_frame='hour',
                            range_color=[0, df['count'].max()*1.0])

    fig.update_layout(
        title="Citibikes Station Frequency Heatmap of New York City by Hour" + bonus,
        updatemenus=[dict(
            type="buttons",
            showactive=False,
            buttons=[
                dict(label="Play",
                     method="animate",
                     args=[None, dict(frame=dict(duration=500, redraw=True), fromcurrent=True)]),
                dict(label="Pause",
                     method="animate",
                     args=[[None], dict(frame=dict(duration=0, redraw=False), mode="immediate", fromcurrent=True)])
            ]
        )],
        sliders = [dict(
            active=0,
            currentvalue={"prefix": "Hour: "},
            pad={"t": 1}
        )],
        height=700, width=700
        
    )

    fig.show(frame=0)

display_interactive_map(interactive_map_data_2018, " (2018)")