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

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

我正在尝试使用等值线绘制地图,但它给了我一个错误,说“color_dicrete_map”是一个意外的关键字。 有人可以帮我吗?

    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()
  • 修复了代码的缩进
  • 来自 OWID 的疫苗接种数据
  • 功能有效。使用 plotly 5.5.0
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")

我解决了!

$ pip install plotly==5.5.0