-1

我正在尝试使用 choropleth 绘制地图,但它给了我一个错误,说“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()
4

2 回答 2

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

在此处输入图像描述

于 2022-01-15T14:54:50.543 回答
0

我解决了!

$ pip install plotly==5.5.0

于 2022-01-15T15:25:05.787 回答