Black Lives Matter. Please consider donating to Black Girls Code today.

Plotly scatter map value error for 'size' property

'm trying to create a scatter map for a dataframe with index= countries and columns= some variables (like below)

data= {'Ss': [14, 14, 30, 878], 'Cs': [20, 116, 321, 3883], 'Rs': [0, 0, 34, 1192]}
affs_df= pd.DataFrame(index= ['Argentina', 'Austria', 'Australia', 'United States'], data= data)

smfig = go.Figure()
col_names = affs_df.columns
for n in col_names:
    smfig.add_trace(go.Scattergeo(
        locationmode = 'country names',
        marker = dict(
            size= affs_df[n],
            line_color= 'rgb(40, 40, 40)',
            line_width= 0.5,
            sizemode= 'area'),
    name= n))

smfig.update_layout(
    title_text= 'Affiliations of publications',
    showlegend= True,
    geo= dict(
        scope= 'world',
        landcolor= 'rgb(217, 217, 217)'))

but I get that error:

ValueError: 
    Invalid value of type 'pandas.core.series.Series' received for the 'size' property of scattergeo.marker

I changed size feature to size= affs_df[n].to_list() but the result comes as an empty map!

Hi @ealy welcome to the forum! To solve the problem of the empty map you need to give the locations argument of go.Scattergeo, see the code below

import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
data= {'Ss': [14, 14, 30, 878], 'Cs': [20, 116, 321, 3883], 'Rs': [0, 0, 34, 1192]}
affs_df= pd.DataFrame(index= ['Argentina', 'Austria', 'Australia', 'United States'], data= data)

smfig = go.Figure()
col_names = affs_df.columns
for n in col_names:
    smfig.add_trace(go.Scattergeo(
        locations=affs_df.index,
        locationmode = 'country names',
        marker = dict(
            size= affs_df[n],
            line_color= 'rgb(40, 40, 40)',
            line_width= 0.5,
            sizemode= 'area'),
    name= n))

smfig.update_layout(
    title_text= 'Affiliations of publications',
    showlegend= True,
    geo= dict(
        scope= 'world',
        landcolor= 'rgb(217, 217, 217)'))
smfig.show()

I could not reproduce the ValueError with the code you pasted, which version of plotly.py are you using?

Alternatively, you can modify the dataframe so that it has the right format for plotly.express (see https://plot.ly/python/scatter-plots-on-maps/)

import plotly.express as px
import pandas as pd
data= {'Ss': [14, 14, 30, 878], 'Cs': [20, 116, 321, 3883], 'Rs': [0, 0, 34, 1192]}
df= pd.DataFrame(index= ['Argentina', 'Austria', 'Australia', 'United States'], data= data)
df = pd.melt(df.reset_index(), id_vars='index', value_vars=['Ss', 'Cs', 'Rs'])
fig = px.scatter_geo(df, locations=df['index'], size=df.value, color=df.variable,
                     locationmode='country names')
fig.update_traces(marker_sizemode='area', marker_sizeref=1)
fig.show()

Thanks @Emmanuelle,
Adding the locations argument solved the problem, my plotly version is 4.3.0.
I prefer using graph_objects as it gives more control over the output.