Home > Net >  Plotly choropleth_mapbox doesn't show map when parsing animation_frame=""
Plotly choropleth_mapbox doesn't show map when parsing animation_frame=""

Time:04-01

fig = px.choropleth_mapbox(Defined_M_rob_data, geojson=merged['geometry'], 
                           color='Crime count',
                           color_continuous_scale = "viridis",
                           locations='EER13CD',                     
                           #animation_frame="Month",
                           center={"lat": 53.13258, "lon": -1.81356},
                           mapbox_style="carto-positron", zoom=5.3)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0,"pad":4})

fig.update_layout(
    title={
        'text': " Robbery in the North East and South West (03/20-02/21)",
        'y':0.98,
        'x':0.45,
        'xanchor': 'auto',
        'yanchor': 'auto'})

fig.show()

I have tried this multiple ways. Each time I can comment out animation_frame and I will get my first month's data density on the map. When I include animation to go through each month (13 in total), I get a tick with no error telling me the code ran successfully and it takes around 2mins.

The issue is that the map does not show up at all. I really can't figure out what I'm doing wrong as the code seems straight forward.

Sample of combined data and geo file: Sample of combined data and geo file Error Logs:

|Timestamp|Level|Message|
|---|---|---|
|Apr 1, 2022, 12:30:23 AM|INFO|Discarding 2 buffered messages for 6930931d-b4b1-46f9-b65c-6bba1ad894ed:6cfa772d26934f8deee6a74d78425003|
|Apr 1, 2022, 12:30:23 AM|INFO|Adapting to protocol v5\.1 for kernel 6930931d-b4b1-46f9-b65c-6bba1ad894ed|
|Apr 1, 2022, 12:30:22 AM|WARNING|tornado\.websocket.WebSocketClosedError|
|Apr 1, 2022, 12:30:22 AM|WARNING|    raise WebSocketClosedError\()|
|Apr 1, 2022, 12:30:22 AM|WARNING|  File "/usr/local/lib/python3\.7/dist-packages/tornado/websocket.py", line 876, in wrapper|
|Apr 1, 2022, 12:30:22 AM|WARNING|    yielded = self\.gen.throw(*exc_info)|
|Apr 1, 2022, 12:30:22 AM|WARNING|  File "/usr/local/lib/python3\.7/dist-packages/tornado/gen.py", line 1141, in run|
|Apr 1, 2022, 12:30:22 AM|WARNING|Traceback \(most recent call last):|
|Apr 1, 2022, 12:30:22 AM|WARNING|During handling of the above exception, another exception occurred:|
|Apr 1, 2022, 12:30:22 AM|WARNING|tornado\.iostream.StreamClosedError: Stream is closed|
|Apr 1, 2022, 12:30:22 AM|WARNING|    value = future\.result()|
|Apr 1, 2022, 12:30:22 AM|WARNING|  File "/usr/local/lib/python3\.7/dist-packages/tornado/gen.py", line 1133, in run|
|Apr 1, 2022, 12:30:22 AM|WARNING|    yield fut|
|Apr 1, 2022, 12:30:22 AM|WARNING|  File "/usr/local/lib/python3\.7/dist-packages/tornado/websocket.py", line 874, in wrapper|
|Apr 1, 2022, 12:30:22 AM|WARNING|Traceback \(most recent call last):|
|Apr 1, 2022, 12:30:22 AM|WARNING|future: \<Future finished exception=WebSocketClosedError()>|
|Apr 1, 2022, 12:30:22 AM|WARNING|ERROR:asyncio:Future exception was never retrieved|
|Apr 1, 2022, 12:30:22 AM|WARNING|tornado\.websocket.WebSocketClosedError|
|Apr 1, 2022, 12:30:22 AM|WARNING|    raise WebSocketClosedError\()|
|Apr 1, 2022, 12:30:22 AM|WARNING|  File "/usr/local/lib/python3\.7/dist-packages/tornado/websocket.py", line 876, in wrapper|
|Apr 1, 2022, 12:30:22 AM|WARNING|    yielded = self\.gen.throw(*exc_info)|
|Apr 1, 2022, 12:30:22 AM|WARNING|  File "/usr/local/lib/python3\.7/dist-packages/tornado/gen.py", line 1141, in run|
|Apr 1, 2022, 12:30:22 AM|WARNING|Traceback \(most recent call last):|
|Apr 1, 2022, 12:30:22 AM|WARNING|During handling of the above exception, another exception occurred:|
|Apr 1, 2022, 12:30:22 AM|WARNING|tornado\.iostream.StreamClosedError: Stream is closed|
|Apr 1, 2022, 12:30:22 AM|WARNING|    value = future\.result()|
|Apr 1, 2022, 12:30:22 AM|WARNING|  File "/usr/local/lib/python3\.7/dist-packages/tornado/gen.py", line 1133, in run|
|Apr 1, 2022, 12:30:22 AM|WARNING|    yield fut|
|Apr 1, 2022, 12:30:22 AM|WARNING|  File "/usr/local/lib/python3\.7/dist-packages/tornado/websocket.py", line 874, in wrapper|
|Apr 1, 2022, 12:30:22 AM|WARNING|Traceback \(most recent call last):|
|Apr 1, 2022, 12:30:22 AM|WARNING|future: \<Future finished exception=WebSocketClosedError()>|
|Apr 1, 2022, 12:30:22 AM|WARNING|ERROR:asyncio:Future exception was never retrieved|
|Apr 1, 2022, 12:30:22 AM|INFO|Starting buffering for 6930931d-b4b1-46f9-b65c-6bba1ad894ed:6cfa772d26934f8deee6a74d78425003|

CodePudding user response:

  • found this API to get crime data enter image description here

    data sourcing

    import geopandas as gpd
    import pandas as pd
    import requests
    
    gdf = gpd.read_file(
        "https://raw.githubusercontent.com/martinjc/UK-GeoJSON/master/json/electoral/gb/topo_eer.json"
    ).set_crs("epsg:4386")
    
    # simplify the geometry
    gdf["geometry"] = gdf.to_crs(gdf.estimate_utm_crs()).simplify(1000).to_crs("epsg:4326")
    
    df = pd.DataFrame()
    # convex_hull gives a single polygon for each region.  it will generate some overlaps, hence maybe some
    # double counting...
    for reg, geom in gdf.set_index("EER13CD").convex_hull.iteritems():
        if reg[0] != "E":
            continue
        ps = ":".join([f"{y},{x}" for x, y in geom.exterior.coords])
        for d in pd.date_range("1-oct-2021", "31-mar-2022", freq="MS").strftime("%Y-%m"):
            params = {"poly": ps, "date": d}
            res = requests.get(
                "https://data.police.uk/api/crimes-street/robbery",
                params=params,
            )
            if res.status_code == 200:
                df = pd.concat(
                    [
                        df,
                        pd.json_normalize(res.json()).assign(EER13CD=reg),
                    ]
                )
            else:
                print(f"failed to get data {reg} {d}")
    
    df.groupby(["EER13CD", "month"], as_index=False).size().rename(
        columns={"size": "Crime count"}
    )
    
  • Related