Not able to change the marker color based on the column value

So in this example of Scatter Map (https://plotly.com/python/scatter-plots-on-maps/),
It shows:

fig = go.Figure(data=go.Scattergeo(
        lon = df['long'],
        lat = df['lat'],
        text = df['text'],
        mode = 'markers',
        marker_color = df['cnt'],
        ))

I can use the columns to show different colors: marker_color = df['cnt']
I applied the same logic to my data, which my data looks something like this:

Address
	postcode	state	country		                           lat	             lon
0	2016	      New South Wales	Australia		-33.892778	151.203901
1	2153	      New South Wales	Australia		-33.758601	150.992887
2	4211	     Queensland	Australia		        -28.033052	153.279625

And my code is:

fig = go.Figure(data=go.Scattergeo(lon=address['lon'], lat=address['lat'],
                                   mode='markers', marker_color=address['state']))

fig.update_layout(title='The Customer Locations')
fig.show()

And I get an error

ValueError: 
    Invalid element(s) received for the 'color' property of scattergeo.marker
        Invalid elements include: ['New South Wales', 'New South Wales', 'Queensland', 'New South Wales', 'Victoria', 'New South Wales', 'New South Wales', 'New South Wales', 'Victoria', 'Queensland']

So if I change to marker_color='red' then just the entire marker changes to red. What am I doing wrong here? I’m so confused…

Thank you!!

Hi @sarahekim53,

The marker_color must be a list of numerical values to be mapped to a colorscale colors. Your code passed a list of strings to the marker_color, not a numerical list.

Hence, you should proceed as follows:

u_states = np.unique(address['state'].values)  #set the array of unique states in your column, address['state']
d = dict(zip(u_states, np.arange(len(u_states))) # a dict that associates a numerical value to each state

Now you can set:

marker_color = [d[s] for s in address['state']]

and so each state will be mapped to a different color on the colorscale (if you did not set any colorscale, Plotly will use the default one: β€˜Plasma’).

1 Like