使用 Python's plotly 在地图上绘制阴影区域

Drawing shaded areas on map with Python's plotly

我正在尝试使用 plotly 在地图上绘制一些阴影区域。坐标包含在 pandas 数据框中,其中每个单元格显示要绘制的特定区域。当我尝试在地图上绘制多个区域时出现问题 - 这是由 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()

如果我尝试 'coordinates': [df.coords.tolist()]'coordinates': [[df.coords[0],df.coords[1],df.coords[2]]],只会绘制一些区域。当我缩小地图时,一些区域消失了。

我做错了什么,我该如何解决?

如何更改阴影区域的外观,例如。透明度?

如何为每个阴影区域添加标签(area1area2area3)?

对于每个区域,您应该添加一个 Scattermapbox 跟踪。这是我的做法:

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()