1

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?

4
  • From which csv/url you're making df ? Also, what is the column increase_12yr ? Commented Dec 30, 2023 at 11:19
  • I'm not sure how that's relevant to my problem, but it's some Zillow housing price data I've done some additional processing on and increase_12yr is just a float field. Commented Dec 30, 2023 at 11:26
  • Whether it's relevant or not to your problem, that makes your example non-reproducible eitherway. Commented Dec 30, 2023 at 11:29
  • You can use the one they used in the link, df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", dtype={"fips": str}), replacing increase_12yr with unemp Commented Dec 30, 2023 at 11:40

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

This looks okay only because that data is pretty uniformly filled out for all counties so the state borders appear pretty consistent. My actual data has more gaps in it, resulting in inconsistent visibility to the state borders. See the first version of the map i posted to see what I mean

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.