Different color scales for different facets of choropleth map

Hi. I made a choropleth map with continuos color scale and divided it for 4 sub-map facets. The problem is that color scale gauge is one for all 4 maps, and even if i’ll try to change color scale it’ll be aplied to all maps together. Is there a way to set different color scaling for different facets?

Solution

fig = px.choropleth(
    df,
    geojson=counties,
    locations="id",
    color="count",
    facet_col="age_group",
    facet_col_wrap=2,
    color_continuous_scale="BuGn",
    hover_name="county",
    width=1000,
    height=900,
    animation_frame="year",
)
fig.update_geos(fitbounds="locations")

# update traces to use different coloraxis
for i, t in enumerate(fig.data):
    t.update(coloraxis=f"coloraxis{i+1}")
for fr in fig.frames:
    # update each of the traces in each of the animation frames
    for i, t in enumerate(fr.data):
        t.update(coloraxis=f"coloraxis{i+1}")

# position / config all coloraxis
fig.update_layout(
    coloraxis={"colorbar": {"x": -0.2, "len": 0.5, "y": 0.8}},
    coloraxis2={
        "colorbar": {
            "x": 1.2,
            "len": 0.5,
            "y": 0.8,
        },
        "colorscale": fig.layout["coloraxis"]["colorscale"],
    },
    coloraxis3={
        "colorbar": {"x": -0.2, "len": 0.5, "y": 0.3},
        "colorscale": fig.layout["coloraxis"]["colorscale"],
    },
    coloraxis4={
        "colorbar": {"x": 1.2, "len": 0.5, "y": 0.3},
        "colorscale": fig.layout["coloraxis"]["colorscale"],
            },
)