How to change colors for NA values to gray in a choropleth map?

Hi @LY_Air,

Another option is to drop all of the rows with NA values and then style the missing countries using layout.geo properties. Here’s an example (in this example I’m just keeping the first 50 contries, you would drop your NA rows there instead).

import plotly.plotly as py
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')

# Filter NA here
# df = df.dropna()
df = df.iloc[:50]

data = [ dict(
        type = 'choropleth',
        locations = df['CODE'],
        z = df['GDP (BILLIONS)'],
        text = df['COUNTRY'],
        colorscale = 'Viridis',
        autocolorscale = False,
        reversescale = True,
        marker = dict(
            line = dict (
                color = 'rgb(180,180,180)',
                width = 0.5
            ) ),
        colorbar = dict(

            tickprefix = '$',
            title = 'GDP<br>Billions US$'),
      ) ]

layout = dict(
    title = '2014 Global GDP<br>Source:\
            <a href="https://www.cia.gov/library/publications/the-world-factbook/fields/2195.html">\
            CIA World Factbook</a>',
    geo = dict(
        landcolor = 'lightgray',
        showland = True,
        showcountries = True,
        countrycolor = 'gray',
        countrywidth = 0.5,
        projection = dict(
            type = 'natural earth'
        )
    )
)

fig = go.FigureWidget( data=data, layout=layout )
fig

Hope that helps,
-Jon

1 Like