How to label animation frame in plotly express?

852 views Asked by At

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?

1

There are 1 answers

0
Rob Raymond On

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="}}])