Empty map without my data px.choropleth (plotly express)

huangapple go评论51阅读模式
英文:

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

Empty map without my data px.choropleth (plotly express)

创建图表的代码:

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

我的输出
Empty map without my data px.choropleth (plotly express)

期望输出
Empty map without my data px.choropleth (plotly express)

英文:

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

Empty map without my data px.choropleth (plotly express)

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

my output
Empty map without my data px.choropleth (plotly express)

expected output
Empty map without my data px.choropleth (plotly express)

答案1

得分: 1

无法显示的原因是缺少设置来确定指定位置字符串的含义:必须指定ISO-3USA-statescountry 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()

Empty map without my data px.choropleth (plotly express)

huangapple
  • 本文由 发表于 2023年6月12日 01:44:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定