Add pie charts on top of figure in Plotly

Viewed 599

I'm attempting to produce a figure similar to the one below (from here) using plotly.go.Scattermapbox, but I don't manage to overlay pie charts on a map in plotly.

Anyone know how to achieve a similar result in plotly?

enter image description here

1 Answers

Not a direct answer to my question, but thought I'd share the work-around that I ended up using instead.

Instead of plotting pie charts, I've plotted concentric circles whose size (either diameter or area) was indicative of the splits by segments. The benefit is that you get to use plotly; the drawback is that some tweaking is necessary to make the reader's perception of bubble sizes match the actual split by segment.

EDIT: added code snippet and screenshot (hovering over one bubble to show the annotated post code):

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

enter image description here

Related