【问题标题】:Plotly Choropleth Map Not Showing UpPlotly Choropleth 地图未显示
【发布时间】:2021-06-15 19:56:05
【问题描述】:

我正在尝试在 Jupyter Notebooks 中显示 Plotly Choropleth Map(我是这类东西的初学者),但由于某种原因它无法正确显示。

我使用的 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)

我不太确定这是否相关!

谢谢大家!

【问题讨论】:

  • 问题是您试图使用 df 中不存在的列。您可以使用的参数是:Country name,Regional indicator,Ladder score,Standard error of ladder score,upperwhisker,lowerwhisker,Logged GDP per capita,Social support,Healthy life expectancy,Freedom to make life choices,Generosity,Perceptions of corruption,Ladder score in Dystopia,Explained by: Log GDP per capita,Explained by: Social support,Explained by: Healthy life expectancy,Explained by: Freedom to make life choices,Explained by: Generosity,Explained by: Perceptions of corruption,Dystopia + residual

标签: python pandas plotly


【解决方案1】:

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

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()

【讨论】:

  • 我们需要按年份对数据进行排序,以使滑块的年份顺序正确。如果我的回答对您有帮助,请采纳为正确答案。
  • 这成功了!非常感谢,我很感激!
  • 很高兴能为您提供帮助。我知道这似乎很多,但请接受我的回答!
猜你喜欢
  • 2022-06-29
  • 1970-01-01
  • 2020-11-09
  • 2018-06-13
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
  • 2021-10-06
  • 1970-01-01
相关资源
最近更新 更多