Map colors different from scale

I can’t figure out why the colors on my map don’t reflect the color scale. Texas should be ~35M. Any tips? Code below

import plotly.plotly as py
import plotly 
plotly.tools.set_credentials_file(username='keryums', api_key='G5YsVJOebOZh0nhwk44q')

data = [ dict(type='choropleth',
              colorscale = 'Viridis',
              autocolorscale=True,
              locations=state_by_sex.state_code,
              z=state_by_sex.veterans,
              locationmode='USA-states'
             )
       ]

layout = dict(title = 'Map of Veteran Suicide Rates by State',
              geo = dict(scope='usa',
                        projection=dict(type ='albers usa'),
                        showlakes = True,
                        lakecolor = 'rgb(255,255,255)')
             )

fig = dict(data=data, layout=layout)
py.iplot(fig, filename = 'd3-choropleth-map')

Hi @keryums,

Does it change anything if you set autocolorscale=False? You generally don’t want to specify a colorscale ('Viridis' in this case) and set autocolorscale to True.

If that doesn’t help, could you add a link to your dataset? Or try to reproduce the problem with this dataset from https://plot.ly/python/choropleth-maps/#united-states-choropleth-map.

-Jon

Hi Jon,

I can recreate the plot you linked to but for some reason its still not working on my dataset, which you can find here. I checked syntax, data types, null values, etc, but can’t find where the issue is coming from. Thanks in advance for your help!

Also, it doesn’t change anything when I specify ‘Viridis’ and change autocolorscale to False.

Hi @keryums

Thanks for providing the dataset. I’m not sure why plotly.js is responding this way, but the problem seems to be that this DataFrame has two rows for each state (one for each sex) and plotly.js doesn’t know how to handle multiple observations per state.

So you’ll want to do some kind of aggregation in pandas before building the choropleth trace. Here’s a full example of computing the sum of veterans per state (across all values of the sex column). Also, I needed to remove autocolorscale=True in order for the Viridis scale to show up.

import plotly.graph_objs as go
import pandas as pd
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()

state_by_sex = pd.read_csv('https://raw.githubusercontent.com/keryums/projects/master/state_by_sex_wide_20190117_1620.csv')

state_veterans = state_by_sex.groupby('state_code', as_index=False).sum()

data = [ dict(type='choropleth',
              colorscale = 'Viridis',
              locations=state_veterans.state_code,
              z=state_veterans.veterans,
              locationmode='USA-states'
             )
       ]

layout = dict(title = 'Map of Veteran Population by State',
              geo = dict(scope='usa',
                        projection=dict(type ='albers usa'),
                        showlakes = True,
                        lakecolor = 'rgb(255,255,255)')
             )

fig = go.Figure(data=data, layout=layout)
iplot(fig)

Hope that helps!
-Jon

Oh gosh, thank you so much! I totally forgot to consider that the dataset has state repeated.