Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Mapbox update choropleth

Hi there, I am having trouble to update colors for the mapbox choropleth.

so I have a year slider, the first call back will use the input to generate a line plot, and the second call back will generate a choropleth map. however, when I interact with the slider, only the first call back function updates the chart, the choropleth doesn’t change color accordingly. anyone knows why?

here’s my code for the mapbox part:

@app.callback(
    dash.dependencies.Output('mapbox', 'figure'),
    [dash.dependencies.Input('year_slider', 'value')])
def update_map(year):
    dt = df[df.year == year].groupby(['incident_zip'])['count'].sum().reset_index()
    dt['bins'] = pd.cut(dt['count'], bins = 20, labels = list(range(20)))

    def get_color(zipcode):
        try:
            b = dt[dt.incident_zip == float(zipcode)]['bins'].tolist()[0]
            converted = round(255 - b*12.75)
            color = 'rgb(255, {}, {})'.format(converted, converted)
        except (KeyError,IndexError):
            color = 'rgb(255, 255, 255)'
        return color

    return{
        "data": [
            dict(
                type = "scattermapbox",
                lat = ["40.650002"],
                lon = ["-73.949997"],
                mode = "markers",
                marker = dict(size = 1),
            )],
        "layout": dict(
            autosize = True,
            hovermode = "closest",
            margin = dict(l = 0, r = 0, t = 0, b = 0),
            mapbox = dict(
                accesstoken = mapbox_access_token,
                bearing = 0,
                center = dict(lat = 40.758896, lon = -73.985130),
                style = "light",
                pitch = 0,
                zoom = 9,
                layers = [{
                    'sourcetype' : 'geojson',
                    'source' : data['features'][i],
                    'color' : get_color(data['features'][i]['properties']['zipcode']), 
                    'type' : 'fill',
                    'opacity' : 0.7} for i in range(len(data['features']))]
            )
        )
    }

also, how do I draw choropleth with a color scale for the value I want to plot? thanks