Medulla Oblongata
Medulla Oblongata

Reputation: 3961

Drawing shaded areas on map with Python's plotly

I'm trying to draw some shaded areas on a map using plotly. The coordinates are contained in a pandas dataframe where each cell shows a specific area to be plotted. A problem arises when I try to draw more than one area on the map - this is defined by coordinates:

import pandas as pd
import plotly.graph_objects as go

# my test dataset
df = pd.DataFrame(data={'names':['area1','area2','area3'], 'coords': 0})
df['coords'] = df['coords'].astype(object)
df.at[0,'coords'] = [[-73.606352888, 45.507489991], [-73.606133883, 45.50687600], [-73.617533258, 45.527512253], [-73.618074188, 45.526759105], [-73.618271651, 45.526500673], [-73.618446320, 45.526287943], [-73.620201713, 45.524298907], [-73.620775593, 45.523650879]]
df.at[1,'coords'] = [[-73.706352888, 45.507489991], [-73.706133883, 45.50687600], [-73.717533258, 45.527512253], [-73.718074188, 45.526759105], [-73.718271651, 45.526500673], [-73.718446320, 45.526287943], [-73.720201713, 45.524298907], [-73.720775593, 45.523650879]]
df.at[2,'coords'] = [[-73.606352888, 45.497489991], [-73.606133883, 45.49687600], [-73.617533258, 45.517512253], [-73.618074188, 45.516759105], [-73.618271651, 45.516500673], [-73.618446320, 45.516287943], [-73.620201713, 45.514298907], [-73.620775593, 45.513650879]]

fig = go.Figure(go.Scattermapbox(mode = "markers", lon = [-73.605], lat = [45.51],))
fig.update_layout(
    mapbox = {'style': "stamen-terrain",
              'center': { 'lon': -73.6, 'lat': 45.5},
              'zoom': 12, 'layers': [{'source': {'type': "FeatureCollection",
                                                 'features': [{'type': "Feature",
                                                               'geometry': {'type': "MultiPolygon",
                                                                            'coordinates': [[df.coords[0],df.coords[1],df.coords[2]]]
#                                                                             'coordinates': [df.coords.tolist()]
                                                                           }
                                                              }]
                                                },
                                      'type': "fill", 'below': "traces", 'color': "royalblue"}]},
    margin = {'l':0, 'r':0, 'b':0, 't':0})
fig.show()

If I try 'coordinates': [df.coords.tolist()] or 'coordinates': [[df.coords[0],df.coords[1],df.coords[2]]], only some areas are plotted. When I zoom out of the map, some of the areas disappear.

What am I doing wrong and how do I fix it?

How do I change the appearance of the shaded areas, eg. transparency?

How do I add labels to each shaded area (area1, area2, area3)?

Upvotes: 0

Views: 389

Answers (1)

Davide_sd
Davide_sd

Reputation: 13150

For each area, you should add a Scattermapbox trace. Here is how I would do it:

import pandas as pd
import plotly.graph_objects as go

# my test dataset
df = pd.DataFrame(data={'names':['area1','area2','area3'], 'coords': 0})
df['coords'] = df['coords'].astype(object)
df.at[0,'coords'] = [[-73.606352888, 45.507489991], [-73.606133883, 45.50687600], [-73.617533258, 45.527512253], [-73.618074188, 45.526759105], [-73.618271651, 45.526500673], [-73.618446320, 45.526287943], [-73.620201713, 45.524298907], [-73.620775593, 45.523650879]]
df.at[1,'coords'] = [[-73.706352888, 45.507489991], [-73.706133883, 45.50687600], [-73.717533258, 45.527512253], [-73.718074188, 45.526759105], [-73.718271651, 45.526500673], [-73.718446320, 45.526287943], [-73.720201713, 45.524298907], [-73.720775593, 45.523650879]]
df.at[2,'coords'] = [[-73.606352888, 45.497489991], [-73.606133883, 45.49687600], [-73.617533258, 45.517512253], [-73.618074188, 45.516759105], [-73.618271651, 45.516500673], [-73.618446320, 45.516287943], [-73.620201713, 45.514298907], [-73.620775593, 45.513650879]]

def get_close_coords(coords):
    c = np.zeros((len(coords) + 1, 2))
    c[:-1, :] = coords
    c[-1, :] = c[0, :]
    return c
    
fig = go.Figure(go.Scattermapbox(mode = "markers", lon = [-73.605], lat = [45.51]))

for i, c in enumerate(df.coords):
    coords = get_close_coords(c)
    fig.add_trace(go.Scattermapbox(lat=coords[:, 1], lon=coords[:, 0], mode="lines", fill="toself", name="Area %s" % i, opacity=0.4))

fig.update_layout(
    mapbox = {
        'style': "stamen-terrain",
        'center': {'lon': -73.6, 'lat': 45.5},
        'zoom': 12,
    },
    margin = {'l':0, 'r':0, 'b':0, 't':0})
fig.show()

Upvotes: 1

Related Questions