Asking this question here after trying on stackoverflow.
I’m running my code on a MacBook Pro, M1, 2020 using a Jupyter Notebook with Python version 3.11.5. I’m using Plotly Express to create an animated chloropleth map that shows which countries were colonized over from 1460 to 1985. Right now, my test data set has 4 countries,–Algeria, Angola, Australia, Belize, and Afghanistan --the colonial status for each country–France, Britain, Portugal, Spain, Decolonized, and Not Colonized–and a range of test (dates aren’t realistic) years for when each country was colonized. The dataset has 3 columns, “country,” “colonizer,” and “year” with the above information in each. Here’s the test dataset I’m using:
| country | colonizer | year |
| ---------- | ----------- | ----- |
| Algeria | France | 1840 |
| Algeria | France | 1841 |
| Algeria | France | 1842 |
| Algeria | France | 1843 |
| Algeria | France | 1844 |
| Algeria | France | 1845 |
| Angola | Portugal | 1840 |
| Angola | Portugal | 1841 |
| Angola | Portugal | 1842 |
| Angola | Portugal | 1843 |
| Angola | Portugal | 1844 |
| Angola | Portugal | 1845 |
| Belize | Spain | 1840 |
| Belize | Spain | 1841 |
| Belize | Spain | 1842 |
| Belize | Spain | 1843 |
| Belize | Spain | 1844 |
| Belize | Spain | 1845 |
| Australia | Britain | 1840 |
| Australia | Britain | 1841 |
| Australia | Britain | 1842 |
| Australia | Britain | 1843 |
| Australia | Decolonized | 1844 |
| Australia | Decolonized | 1845 |
| Afghanistan | Not Colonized | 1840 |
| Afghanistan | Not Colonized | 1841 |
| Afghanistan | Not Colonized | 1842 |
| Afghanistan | Britain | 1843 |
| Afghanistan | Britain | 1844 |
| Afghanistan | Britain | 1845 |
And I’m plotting the test data using the following code:
import pandas as pd
import plotly.express as px
# Add a color mapping for each colonizer
colonizer_color = {
'Britain': 'blue',
'France': 'red',
'Spain': 'yellow',
'Portugal': 'green',
'Belgium': 'purple',
'Germany': 'gray',
'Italy': 'orange',
'Decolonized': 'black',
'Not Colonized': 'aliceblue'
}
# Create the choropleth map
fig = px.choropleth(
col_sort_df,
locations='country', # Column name for countries
locationmode='country names',
color='colonizer', # Column name for the color mapping
hover_name='country',
animation_frame='year', # Column name for the year to animate
animation_group='year', # from stackoverflow answer
color_discrete_map=colonizer_color,
projection='natural earth', # Projection style for the map
title='Colonization Over Time'
)
# Update layout for a better appearance
fig.update_layout(
geo=dict(
showframe=False,
showcoastlines=False
)
)
# Show the figure
fig.show()
I expected the code to show all 5 countries at the years they overlap, color coded by the colonizer at each year. Instead, the map only shows only three color coded categories of colonial status. Is there some way to get the chloropleth map to show all 5 countries, color coded by colonizer, at the same time?