Show state borders but not county borders on US Choropleth map

93 views Asked by At

I have some US county-level data and I want a choropleth map that does show state borders but not county borders. Following this solution, I end up with this:

enter image description here

As you can see it seems the state borders are layered below the data. My attempt to solve this was to make a second choropleth with the state borders and a transparent colorscale, and layer that on top of the map with the data. First here's the borders-only figure:

state_borders = px.choropleth(df,
   geojson=counties,
   locations='fips',
   color='increase_12yr',
   color_continuous_scale=["rgba(1,1,1,0)" ,"rgba(1,0,0,0)"],
   range_color=(0, 350),
    center = {"lat": 37.0902, "lon": -95.7129},
    scope='usa',
    basemap_visible=False
)
state_borders.update_traces(marker_line_width=0, marker_opacity=1)
state_borders.update_geos(
showsubunits=True, subunitcolor="black"
state_borders.show()
)

enter image description here

So far so good, but when I try to add this as a trace to the original map, I end up with this:

enter image description here

Here is the full code minus my data specific stuff:

with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response)

fig = px.choropleth(df,
   geojson=counties,
   locations='fips',
   color='increase_12yr',
   color_continuous_scale="Reds",
   range_color=(0, 350),
    center = {"lat": 37.0902, "lon": -95.7129},
    scope='usa',
    basemap_visible=False
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

state_borders = px.choropleth(df,
   geojson=counties,
   locations='fips',
   color='increase_12yr',
   color_continuous_scale=["rgba(1,1,1,0)" ,"rgba(1,0,0,0)"],
   range_color=(0, 350),
    center = {"lat": 37.0902, "lon": -95.7129},
    scope='usa',
    basemap_visible=False
)
state_borders.update_traces(marker_line_width=0, marker_opacity=1)
state_borders.update_geos(showsubunits=True, subunitcolor="black")

fig.add_trace(state_borders.data[0])

fig.show()

So anyone know how I can layer the state borders on top of my data?

2

There are 2 answers

1
Timeless On

Using a single choropleth map :

fig = px.choropleth(
    df, geojson=counties,
    locations="fips", color="unemp",
    color_continuous_scale="Reds",
    range_color=(0, 12),
    center = {"lat": 37.0902, "lon": -95.7129},
    scope="usa",
)

# ajdust the parameters/arguments if necessary
fig.update_layout(margin={"r":0, "t":0, "l":0, "b":0})
fig.update_traces(marker_line_width=0, marker_opacity=0.7)
fig.update_geos(showsubunits=True, subunitcolor="black")

fig.show()

enter image description here

0
Derek O On

It's not perfect, but you can use the built-in argument scope="usa" in the fig.update_geos method.

fig.update_geos(
    visible=False, resolution=50, scope="usa",
    showcountries=True, countrycolor="Black",
    showsubunits=True, subunitcolor="Black", subunitwidth=1.5,
)

Fully reproducible example (with null regions included):

from urllib.request import urlopen
import json
import pandas as pd
import plotly.express as px

with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response)

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", dtype={"fips": str})

max_value = df.unemp.max()
df.iloc[200:700, [1]] = None

fig = px.choropleth(df,
   geojson=counties,
   locations='fips',
   color='unemp',
   color_continuous_scale="Reds",
   range_color=(0, max_value),
    center = {"lat": 37.0902, "lon": -95.7129},
    scope='usa',
    basemap_visible=False
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.update_traces(marker_line_width=0, marker_opacity=1)
fig.update_geos(
    visible=False, resolution=50, scope="usa",
    showcountries=True, countrycolor="Black",
    showsubunits=True, subunitcolor="Black", subunitwidth=1.5,
)

enter image description here