Let's take a sample example of animated scatter graph from plotly site :
import plotly.express as px
df = px.data.gapminder()
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.show()
I would like to add an animated horizontal line that represents the weighted average life expectancy for each year. I can create a list with those weighted average life expectancy like that :
def weighted_average(df, values, weights):
# source : https://datagy.io/pandas-weighted-average/
return sum(df[weights] * df[values]) / df[weights].sum()
L_weighted_average_life_exp = df.groupby('year').apply(weighted_average, 'lifeExp', 'pop').to_list()
We can add an horizontal line using add_hline. I guess that I have to iterate over fig.frames
but I don't know how. I tried something like :
for y,frame in zip(L_weighted_average_life_exp,fig.frames):
frame.add_hline(y=y, line_width=1, line_dash="dash", line_color="black")
But 'Frame' object has no attribute 'add_hline'.
Would you know please how to do ?
Bonus : add an annotation next to the horizontal line with the amount of the weighted average life expectancy (so the value of y-axis of the horizontal line)
I found a non-optimal solution, which consists in adding scatters having the form line-ew
:
import plotly.express as px
import pandas as pd
import numpy as np
def weighted_average(df, values, weights):
return sum(df[weights] * df[values]) / df[weights].sum()
df = px.data.gapminder()
df["Point category"] = "Country"
size = df["pop"].mean()
for index, row in pd.DataFrame(df.groupby('year').apply(weighted_average, 'lifeExp', 'pop')).iterrows():
i = 100
while i < df["gdpPercap"].max() :
for ii in [1,5]:#range(1,10):
df.loc[df.index.max() + 1] = ["Weighted average life exp %s %s"%(i,ii),"Weighted average life exp",index,row[0],size,i*ii,np.nan,np.nan,"Weighted average life exp"]
i = i*10
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,symbol = df["Point category"],range_x=[100,100000], range_y=[25,90],
symbol_sequence=["circle","line-ew"],color_discrete_sequence=px.colors.qualitative.Dark24)
fig.update_traces(marker=dict(line=dict(width=1,color='Black')))
fig.show()