简体   繁体   中英

How to label animation frame in plotly express?

I am using plotly express in python

I have a px.choropleth figure with a slider using animation_frame

The label for animation frame is currently animation_frame=

I would like to change it to 'Year'

How can I change the label of the animation_frame?

Simple answer:

fig.update_layout(sliders=[{"currentvalue": {"prefix": "Year="}}])

full working example

import io, requests
import plotly.express as px
import pandas as pd

dfraw = pd.read_csv(
    io.StringIO(
        requests.get(
            "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/vaccinations.csv"
        ).text
    )
)
dfraw["date"] = pd.to_datetime(dfraw["date"])

df = (
    dfraw.assign(year=lambda d: d["date"].dt.year)
    .groupby(["iso_code", "year"], as_index=False)
    .agg({"date": "last", "people_fully_vaccinated": "last"})
).sort_values(["year","iso_code"])

fig = px.choropleth(
    df, locations="iso_code", color="people_fully_vaccinated", animation_frame="year"
)

fig.update_layout(sliders=[{"currentvalue": {"prefix": "Year="}}])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM