在 Plotly 中的图形顶部添加饼图

Add pie charts on top of figure in Plotly

我正在尝试使用 plotly.go.Scattermapbox 生成类似于下面的图(来自 here),但我无法在 [=11] 中的地图上叠加饼图=].

有人知道如何在 plotly 中获得类似的结果吗?

不是我的问题的直接答案,但我认为我会分享我最终使用的解决方法。

我没有绘制饼图,而是绘制了同心圆,其大小(直径或面积)表示分段分割。好处是你可以使用 plotly;缺点是需要进行一些调整才能使 reader 对气泡大小的感知与实际的分段匹配。

编辑:添加了代码片段和屏幕截图(将鼠标悬停在一个气泡上以显示带注释的 post 代码):

fig = go.Figure()
segment_names = ["Segment 1", "Segment 2", "Segment 3"]
segment_radii = ["segment_1_size", "segment_2_size", "segment_3_size"]
segment_colours = ["forestgreen", "darkslategray", "lightgreen"]


# Plot all segments
for name, radius, colour in zip(
    segment_names, segment_radii, segment_colours
):
    segment_trace = go.Scattermapbox(
        lon=df["longitude"],
        lat=df["latitude"],
        mode="markers",
        marker=go.scattermapbox.Marker(
            size=df[radius] * 2,
            sizemode="diameter",
            color=colour,
            opacity=0.8
        ),
        name=name,
        hoverinfo="text",
        text=df["outward_post_code"],
        # ^ first characters of UK post code
    )
    fig.add_trace(segment_trace)

# Update figure
fig.update_layout(
    mapbox_style="carto-positron",
    title_text="An interesting chart with bubbles & segments",
    width=800,
    height=600,
    xaxis_title="Longitude",
    yaxis_title="Latitude",
    margin=dict(b=0, t=0, l=0, r=0),
    legend=dict(yanchor="bottom", y=0.02, xanchor="left", x=0.02),
)
fig.show()