绘声绘色的Choropleth地图未显示

2024-10-02 20:35:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图在Jupyter笔记本中显示一个有情节的Choropleth地图(我是这类东西的初学者),但由于某些原因,它无法正确显示

我使用的csv文件可在此处找到:

https://www.kaggle.com/ajaypalsinghlo/world-happiness-report-2021

下面是通向choropleth的代码:

# here we're assigning the hover data columns to use for our choropleth map below

hover_data_cols_df = ['Country', 'Life Ladder', 'Log GDP per capita', 'Social support', 'Healthy life expectancy at birth', 'Freedom to make life choices', 'Generosity', 'Perceptions of corruption']
df.groupby('Year').Country.count()

下面是实际的choropleth的代码:

choropleth_map = px.choropleth(df, 
                locations="Country",
                color='Life Ladder', 
                hover_name = 'Life Ladder',
                hover_data = hover_data_cols_df,
                color_continuous_scale = px.colors.sequential.Oranges,
                animation_frame="Year"
               ).update_layout (title_text = 'World Happiness Index - year wise data', title_x = 0.5,);
iplot(choropleth_map)

我目前没有收到任何附加到它的错误消息,但是,当我在浏览器上检查控制台日志时,我确实发现此错误:

Wolrd-Happiness-Report.ipynb:1 Uncaught ReferenceError: require is not defined
at <anonymous>:1:17
at t.attachWidget (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at t.insertWidget (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at x._insertOutput (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at x.onModelChanged (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at m (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at Object.l [as emit] (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at e.emit (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at c._onListChanged (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)
at m (jlab_core.64abc115a1efeec58694.js?v=64abc115a1efeec58694:2)

我不太确定这是否相关

谢谢大家


Tags: to代码mapdfdatajscountryat
1条回答
网友
1楼 · 发布于 2024-10-02 20:35:13

您的任务需要将国家名称与地图上的国家关联起来的设置。它要求位置模式为国家/地区名称

import pandas as pd

df = pd.read_csv('./data/world-happiness-report.csv', sep=',')
df.sort_values('year', ascending=True, inplace=True)
hover_data_cols_df = ['Country name', 'year', 'Life Ladder', 'Log GDP per capita', 'Social support', 'Healthy life expectancy at birth', 'Freedom to make life choices', 'Generosity', 'Perceptions of corruption']
import plotly.express as px

fig = px.choropleth(df,
                    locations="Country name",
                    locationmode='country names',
                    color='Life Ladder', 
                    hover_name = 'Life Ladder',
                    hover_data = hover_data_cols_df,
                    color_continuous_scale = px.colors.sequential.Oranges,
                    animation_frame="year"
               )
fig.update_layout (title_text = 'World Happiness Index - year wise data', title_x = 0.5,);

fig.show()

enter image description here

相关问题 更多 >