英文:
Empty map without my data px.choropleth (plotly express)
问题
以下是您要翻译的内容:
我尝试创建px.choropleth来显示各个国家的平均工资。
但我的代码返回了一个没有任何数据的地图。
我已经多次检查了我的代码。
它看起来很好,但不起作用。
你能告诉我问题在哪里吗?
数据准备代码:
df['employee_residence_full'] = df['employee_residence'].map(ISO3166)
df['company_location_full'] = df['company_location'].map(ISO3166)
# 此处使用“map()”函数,将ISO3166字典应用于“company_location”列中的每个值,
# 将字符串国家名称替换为其ISO3166代码。
result = df.groupby('company_location_full').agg(
{'salary_in_TH_usd':['mean', 'median'],
'company_location_full': 'count'})
result = result.round(2)
result.columns = ['_'.join(col).strip() for col in result.columns.values]
result = result.reset_index()
result
创建图表的代码:
fig = px.choropleth(locations=result['company_location_full'],
color=result['salary_in_TH_usd_median'],
color_continuous_scale=px.colors.sequential.solar,
template='plotly_dark',
title = 'Average Salary by Company Location')
fig.update_layout(font = dict(size=17,family="Franklin Gothic"))
fig.show();
英文:
I try to create px.choropleth to show average salaries by Countries.
But my code returned map without any data.
I've checked my ode several times.
It looks good but don't work.
Coul you tell me what's the probleb?
Data preparation code:
df['employee_residence_full'] = df['employee_residence'].map(ISO3166)
df['company_location_full'] = df['company_location'].map(ISO3166)
# The "map()" function is used here to apply the ISO3166 dictionary to each value in the
# "company_location" column, replacing the string country name with its ISO3166 code.
result = df.groupby('company_location_full').agg(
{'salary_in_TH_usd':['mean', 'median'],
'company_location_full': 'count'})
result = result.round(2)
result.columns = ['_'.join(col).strip() for col in result.columns.values]
result = result.reset_index()
result
Code for creating the plot:
fig = px.choropleth(locations=result['company_location_full'],
color=result['salary_in_TH_usd_median'],
color_continuous_scale=px.colors.sequential.solar,
template='plotly_dark',
title = 'Average Salary by Company Location')
fig.update_layout(font = dict(size=17,family="Franklin Gothic"))
fig.show();
答案1
得分: 1
无法显示的原因是缺少设置来确定指定位置字符串的含义:必须指定ISO-3
、USA-states
或country names
之一。如果这是您的数据,您需要country names
。
import plotly.express as px
fig = px.choropleth(result,
locations='company_location_full',
locationmode='country names', # 更新
color='salary_in_TH_usd_median',
color_continuous_scale=px.colors.sequential.solar,
template='plotly_dark',
title='Average Salary by Company Location')
fig.update_layout(font=dict(size=17, family="Franklin Gothic"))
fig.show()
英文:
The reason it is not displayed is that there is a missing setting to determine what the specified location string specifies: one of ISO-3
, USA-states
, or country names
must be specified. If it is your data, you need country names
.
import plotly.express as px
fig = px.choropleth(result,
locations='company_location_full',
locationmode='country names', # update
color='salary_in_TH_usd_median',
color_continuous_scale=px.colors.sequential.solar,
template='plotly_dark',
title = 'Average Salary by Company Location')
fig.update_layout(font=dict(size=17, family="Franklin Gothic"))
fig.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论