如何获得 Plotly Express 动画叶绿素地图来显示每年多个国家/地区?

问题描述 投票:0回答:1

我使用 Python 版本 3.11.5 的 Jupyter Notebook 在 MacBook Pro M1 2020 上运行我的代码。我正在使用 Plotly Express 创建一个动画叶绿体地图,显示从 1632 年到 1981 年哪些国家被殖民。现在,我的数据集有 2 个国家,--阿尔及利亚和澳大利亚--每个国家的殖民者的名字- -法国和英国 - 以及每个国家被殖民的一系列年份 - 1848-1962 和 1829-1919。该数据集有 3 列,“国家”、“殖民者”和“年份”,每列均包含上述信息。以下是每个国家/地区 10 年数据的子集:

| country  | colonizer | year |
| -------- | --------  | ---- |               
| Algeria  | France    | 1848 |
| Algeria  | France    | 1849 |
| Algeria  | France    | 1850 |
| Algeria  | France    | 1851 |
| Algeria  | France    | 1852 |
| Algeria  | France    | 1853 |
| Algeria  | France    | 1854 |
| Algeria  | France    | 1855 |
| Algeria  | France    | 1856 |
| Algeria  | France    | 1857 |
| Algeria  | France    | 1858 |
| Australia| Britain   | 1839 |
| Australia| Britain   | 1840 |
| Australia| Britain   | 1841 |
| Australia| Britain   | 1842 |
| Australia| Britain   | 1843 |
| Australia| Britain   | 1844 |
| Australia| Britain   | 1845 |
| Australia| Britain   | 1846 |
| Australia| Britain   | 1847 |
| Australia| Britain   | 1848 |
| Australia| Britain   | 1849 |

我按年份对数据集进行排序

col_sort_df = colonized_df.sort_values(by = ['Year'])
,并期望代码显示阿尔及利亚,然后是澳大利亚,每年由殖民者进行颜色编码。相反,地图上只显示了一个殖民者英国和一个国家澳大利亚。这是我在排序数据集上使用的代码:

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',
    'Netherlands': 'black'
}

# 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 over time
    color_discrete_map=colonizer_color,
    projection="natural earth",  # Projection style for the map
    title="Colonization by England and France (1800s-Present)"
)

# Update layout for a better appearance
fig.update_layout(
    geo=dict(
        showframe=False,
        showcoastlines=False
    )
)

# Show the figure
fig.show()
python plotly
1个回答
0
投票

在这种情况下,我认为添加动画组可以解决问题。如果未设置,将使用统治殖民地国家的类别变量创建框架,从而产生有趣的动画。

import pandas as pd
import plotly.express as px

fig = px.choropleth(df,
                    locations="country",
                    locationmode="country names",
                    color="colonizer",
                    hover_name="country",
                    animation_frame="year",
                    animation_group='year',# update
                    color_discrete_map=colonizer_color,
                    projection="natural earth",
                    title="Colonization by England and France (1800s-Present)"
                   )

fig.update_layout(
    geo=dict(
        showframe=False,
        showcoastlines=False)
)
fig.update_layout(height=500, width=800)
fig.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.