79728819

Date: 2025-08-07 15:38:15
Score: 1
Natty:
Report link

This is very useful to me, thanks! I took the code from the accepted answer above and generalized it a bit, so that it works with any number of interiors, and only needs one call to fig.add_trace:


import plotly.graph_objects as go
from shapely import Point

# Make some circles
shape_a = Point(0, 0).buffer(10)  # Exterior shape
shape_b = Point(0, 0).buffer(2)  # interior hole
shape_d = Point(5, 0).buffer(1)  # interior hole

# subtract holes using shapely
shape_c = shape_a - shape_b - shape_d

# The exterior shape gets added to the coordinates first
x, y = (list(c) for c in shape_c.exterior.xy)
for interior in shape_c.interiors:
    # `None` denotes separated loops in plotly
    x.append(None)
    y.append(None)

    # Extend with each interior shape
    ix, iy = interior.xy
    x.extend(ix)
    y.extend(iy)

fig = go.Figure(layout=go.Layout(width=640, height=640))
fig.add_trace(go.Scatter(x=x, y=y, fill="toself"))
fig.show()
Reasons:
  • Blacklisted phrase (0.5): thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Blair