Search code examples
plotlychoropleth

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

dataset

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 my output

expected output expected output


Solution

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

    enter image description here