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:
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()
)
So far so good, but when I try to add this as a trace to the original map, I end up with this:
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?





df? Also, what is the columnincrease_12yr?df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", dtype={"fips": str}), replacing increase_12yr with unemp