Plotly for Python 可以绘制一个带有一个或多个孔的多边形吗?

Can Plotly for Python plot a polygon with one or multiple holes in it?

一个简单的例子是

shape_a = Point(0,0).buffer(10)
shape_b = Point(0,0).buffer(5)
shape_c = shape_a-shape_b

更新: 出色地。 fill='toself' 也有效,如果孔的边界坐标在相反的时钟方向附加(由 None 分隔)到外部坐标。

我自己找到了解决方法,使用 fill='tonext'。 先决条件是您需要在外部边界之前绘制内部边界。 在多个孔的情况下,您必须首先通过坐标列表中的 'None' 将它们连接到一个散点对象中。

shape_a = Point(0,0).buffer(10)
shape_b = Point(0,0).buffer(2)
shape_d = Point(5,0).buffer(1)

shape_c = shape_a - shape_b
shape_c = shape_c - shape_d

fig = go.Figure(layout=go.Layout(width=640, height=640))
x_a, y_a = shape_a.exterior.xy
x_b, y_b = shape_b.exterior.xy
x_d, y_d = shape_d.exterior.xy
int_x = list(x_b) +  [None] + list(x_d)
int_y = list(y_b) +  [None] + list(y_d)


fig.add_trace(go.Scatter(x=list(int_x), y=list(int_y), fill='none', showlegend=False))
fig.add_trace(go.Scatter(x=list(x_a), y=list(y_a), name='polygon_with_holes', fill='tonext'))
  • 通常我将几何体(形状 对象)与映射几何体
  • 联系起来
  • geojson 分层到 mapbox trace
  • 上很简单
  • 下面展示了带有多个孔的附加对象
from shapely.geometry import Point
import plotly.express as px
import numpy as np

shape_a = Point(3, 3).buffer(10)
shape_b = Point(3, 3).buffer(5)
shape_c = shape_a - shape_b

shape_2 = Point(15,15).buffer(10)
for p in [Point(a,b).buffer(2) for a,b in zip(np.linspace(8, 22, 8), np.linspace(8,22, 4))]:
    shape_2 = shape_2 - p



px.scatter_mapbox(lat=[0], lon=[0], mapbox_style="carto-positron").update_layout(
    mapbox={
        "zoom": 2,
        "layers": [
            {"source": shape_c.__geo_interface__, "type": "fill", "color": "green"},
            {"source": shape_2.__geo_interface__, "type": "fill", "color": "yellow"},

        ],
    }
)