I'm looking to add in interactive text (like a KPI) to show a variable on a graph. Below is an example in the plotly docs on a scatterplot with a slider. I'd like to do this, but also add text at the top to say what the average population is at any given year and update accordingly. Ex: for slider at year 1952, the top of the graph would say 16950402. I would also like to keep this in plotly express.
I know I could just use a hover label for this, but I'd prefer to have some big text at the top of the graph.
Does anyone know how to do this? I know adding graph titles or annotations with variables is possible, but I don't know how to get it to update with the slider.
import plotly.express as px
df = px.data.gapminder()
text = df.groupby("year").pop.mean().round(0)
fig = px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
size="pop", color="continent", hover_name="country",
log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90])
fig["layout"].pop("updatemenus") # optional, drop animation buttons
fig.show()
Here is an example of my desired output, but I'd like the title/text to change with the slider:
