How to color geojson map for a scattermap?

I’m using a Puerto Rico geojson for a scatter map, and I’m passing the coordinates into Scattergeo. Is it possible for me to color the map this way? I have the color property inside of the geojson file. I just want the map to look similar to plotly’s default scattermap.

# load pr json
with open('../puerto_rico.geojson') as f:
    pr_geojson = json.load(f)

# get boundaries for puerto rico
pts = []
for feature in pr_geojson['features']:
    if feature['geometry']['type'] == 'Polygon':
        pts.extend(feature['geometry']['coordinates'][0])
        pts.append([None, None])#mark the end of a polygon

    elif feature['geometry']['type'] == 'MultiPolygon':
        for polyg in feature['geometry']['coordinates']:
            pts.extend(polyg[0])
            pts.append([None, None])#end of polygon
    elif feature['geometry']['type'] == 'LineString':
        points.extend(feature['geometry']['coordinates'])
        points.append([None, None])
    else: pass
x, y = zip(*pts)

data = []
# create pr map 
data.append(
    go.Scattergeo(
        lon = x,
        lat = y,
        mode="lines",
        line_width=1.5,
        line_color="blue",
    )
)

fig = go.Figure(data=data)

fig.update_layout(height=800, margin={"r":0,"t":0,"l":0,"b":0})
fig.update_geos(visible=False)

fig.show()

@bestfirstsearch
Do you want to color the counties/regions? If this is the case you shoud define a go.Choropleth or px.choropleth, not the boundary as a scatter plot, because with choropleth you get automatically the country boundary.

I just want to color the whole country. Would I still be able to plot scatter points with go.Choropleth? Similar to go.Scattergeo?

Your question isn’t too clear.

  1. What you want to color?
  2. If you want to keep the above posted country boundary and to color some subregions, then the best idea is to plot the colored counties/regions as shapes. If you serach this forum you should find such an example, I posted a few years ago.

My apologies, I should I have clarified. I want to fill in the entire country with a single color, no subregions. So the white space inside the outlines should be blue.

Since the outline is just coordinates, I’m wondering if it’s possible color inside this region?

In this case it is straightforward. In your above go.Scattergeo definition, insert

fill="toself"
fillcolor="lightblue" # the color for the country area

That’s all!

Thank you!

1 Like