0

I'm using plotly express scattergeo to plot some data points on a world map but I also like to have the US be shown at the state level. The 'world' scope doesn't react to the showsubunits argument when I use:

fig.update_geos(
   visible=True, resolution=50, scope='world',
   showcountires=True, countrycolor="Black",
   showsubunits=True, subunitcolor='Brown')

However, the 'usa' scope does and shows the state lines when I limit my scope only to US. Is there a way to somehow overlay a world map with only country borders and also a US map with state borders for the same set of scatter lat/lon data? As a more general question, can I selectively define which countries to show at the state level?

MikeRD
  • 23
  • 4

1 Answers1

1
import requests
import numpy as np
import plotly.express as px
import plotly.graph_objects as go

states_geojson = requests.get(
    "https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_110m_admin_1_states_provinces_lines.geojson"
).json()


df = px.data.gapminder().query("year == 2007")
fig = px.scatter_geo(
    df,
    locations="iso_alpha",
    size="pop",  # size of markers, "pop" is one of the columns of gapminder
)


fig = fig.add_trace(
    go.Scattergeo(
        lat=[
            v
            for sub in [
                np.array(f["geometry"]["coordinates"])[:, 1].tolist() + [None]
                for f in states_geojson["features"]
            ]
            for v in sub
        ],
        lon=[
            v
            for sub in [
                np.array(f["geometry"]["coordinates"])[:, 0].tolist() + [None]
                for f in states_geojson["features"]
            ]
            for v in sub
        ],
        line_color="brown",
        line_width=1,
        mode="lines",
        showlegend=False,
    )
)

fig.update_geos(
    visible=True, resolution=50, scope="world", showcountries=True, countrycolor="Black"
)

enter image description here

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30