expressplotlychoroplethdiscrete

TypeError: choropleth() got an unexpected keyword argument 'color_discrete_map'


I am trying to plot a map using choropleth, but it gives me an error that said "color_dicrete_map" is an unexpected keyword. Can anyone help me with it?

    def plot_vaccin(color, vaccin):
fig = px.choropleth(country_latest, locations="iso_code",
                    color=vaccin,
                    hover_name="country",
                    color_discrete_map={True: color, False: 'lightgrey'})

layout = go.Layout(
    title=go.layout.Title(
        text= f"<b>Countries using {vaccin} vaccin</b>",
        x=0.5
    ),
    showlegend=False,
    font=dict(size=14),
    width = 750,
    height = 350,
    margin=dict(l=0,r=0,b=0,t=30)
)

fig.update_layout(layout)

fig.show()

Solution

  • import pandas as pd
    import plotly.graph_objects as go
    import plotly.express as px
    
    def plot_vaccin(color, vaccin):
        fig = px.choropleth(
            country_latest,
            locations="iso_code",
            color=vaccin,
            hover_name="country",
            color_discrete_map={True: color, False: "lightgrey"},
        )
    
        layout = go.Layout(
            title=go.layout.Title(text=f"<b>Countries using {vaccin} vaccin</b>", x=0.5),
            showlegend=False,
            font=dict(size=14),
            width=750,
            height=350,
            margin=dict(l=0, r=0, b=0, t=30),
        )
    
        fig.update_layout(layout)
    
        fig.show()
    
    country_latest = (
        pd.read_csv(
            "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/vaccinations.csv",
            parse_dates=["date"],
        )
        .sort_values(["iso_code", "date"], ascending=[1, 0])
        .groupby("iso_code", as_index=False)
        .first()
        .rename(columns={"location":"country"})
    )
    plot_vaccin("red", "people_fully_vaccinated_per_hundred")
    

    enter image description here