nabzzzz
nabzzzz

Reputation: 21

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?

Upvotes: 1

Views: 992

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31166

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

Upvotes: 1

Related Questions