Black Lives Matter. Please consider donating to Black Girls Code today.

How can I combine Choropleth and Scatter layer with animation frame in a plotly map

I combine Choropleth and Scatter map but the animation frame does not synchronize the data.
The animation frame on the Year only works with the scatter but does not change the data on the choropleth

fig = px.scatter_geo(dataset, locations="iso_alpha",
                    hover_name="Country", text = "Country",size="Value_Air",
                     animation_frame="Year", animation_group="Country",
                    )

fig_px = px.choropleth(dataset,    
                       locations = "iso_alpha",
                       color="Value_Co2", 
                       hover_name="Country", 
                       animation_frame="Year",
                       #animation_group="Country",
                       hover_data={'iso_alpha':False, 
                                  })

fig.add_trace(
    fig_px.data[0]
)
fig.show()

.

The animation frame on the date change only the scatter but not change the country background.
how should i do? can anyone help?

Hi @kikkaf welcome to the forum! Animation frames are stored in fig.frames and not in fig.data, and you need to merge the frames of the two figures as in the code below (in addition to adding the trace). Please read the tutorial on animations and the brand new tutorial on the structure of figures. Also if you do print(fig) you can inspect the structure of your figure and see that the figure has frames.

import plotly.express as px

df = px.data.gapminder()
fig = px.choropleth(df, locations="iso_alpha",
                    color="lifeExp", # lifeExp is a column of gapminder
                    hover_name="country", # column to add to hover information
                    color_continuous_scale=px.colors.sequential.Plasma,
                    animation_frame='year')
fig2 = px.scatter_geo(df, locations="iso_alpha",
                    size="lifeExp", # lifeExp is a column of gapminder
                    hover_name="country", # column to add to hover information
                    color_continuous_scale=px.colors.sequential.Plasma,
                    animation_frame='year')
fig.add_trace(fig2.data[0])
for i, frame in enumerate(fig.frames):
    fig.frames[i].data += (fig2.frames[i].data[0],)
fig.show()
2 Likes

It works, thanks for your help

fig_px = px.choropleth(dataset,    
                       locations = "iso_alpha",
                       color="Value_Co2", 
                       hover_name="Country",
                       color_continuous_scale='Reds',
                       animation_frame="Year",
                       hover_data={'iso_alpha':False, 
                                  })

I have a similar problem with color_continuous_scale in choropleth graph,
When I combine the two graphs, it puts me the default color and not ‘Reds’.
Do you have any suggestions?

It’s because the colorscale is defined in the coloraxis, which itself is part of the layout of the figure, so you need to take the coloraxis and add it to the layout of the other figure. For such a case I would recommend to do a print(fig) in order to understand well the structure of the two figures, and take everything you need from the second figure to put it in the first one.

1 Like

sorry, how do I click on a scatter and open another chart under the map, with country as a parameter?

How can I name the trace since fig.add_trace(fig2.data[0], name=“Trace 1” ) does not work. It results in an error: 'TypeError: add_trace() got an unexpected keyword argument ‘name’

Also, even the default trace name doesn’t initially show up on the Legend. But, eventually, it does. Here is a link showing the problem:

https://drive.google.com/file/d/1D4-YdC6n_E0_Oe21X5iknrqwrPNFYeTt/view?usp=sharing

Thanks!