matplotlib 我如何从包含多个x,y坐标数组的列表中绘制多个形状优美的多边形?

pobjuy32  于 2022-11-30  发布在  其他
关注(0)|答案(1)|浏览(114)

我想在同一个图形中绘制多个形状优美的多边形。我将多个多边形的坐标存储在多个数组中,这些数组存储在列表“标签”中,如下所示:

labels[0:2]
[array([[-30.11413383,  22.57654762],
    [-30.11402512,  22.57665825],
    [-30.11270714,  22.57665825],
    ...,
    [-30.11391449,  22.57588959],
    [-30.11391449,  22.57610893],
    [-30.11413383,  22.57632828]]),
 array([[-30.06881332,  22.57555962],
    [-30.06837463,  22.57555962],
    [-30.06815529,  22.57534027],
    ...,
    [-30.06969261,  22.57512093],
    [-30.06947136,  22.57534027],
    [-30.06903267,  22.57534027]])]

我可以用下面的代码分别绘制列表“标签”中的每个元素,但是我想在一个图表中绘制列表“标签”中的所有元素。有办法做到吗?

import matplotlib.pyplot as plt
from shapely.geometry.polygon import Polygon as Pol_s
x,y = Pol_s(mesangium_label[1]).exterior.xy
plt.plot(x,y)
lf3rwulv

lf3rwulv1#

当然,您可以循环遍历labels的元素,并将它们全部绘制在同一个matplotlib axes对象上:

fig, ax = plt.subplots()
for line in labels:
    x = line[:, 0]
    y = line[:, 1]
    ax.plot(x, y)

或者,您可以使用geopandas将几何图形作为GeometryArray来处理。

import shapely.geometry, pandas as pd, geopandas
df = pd.DataFrame({'geometry': labels})
df['geometry'] = df['geometry'].apply(shapely.geometry.LineString)
df = geopandas.GeoDataFrame(df)
df.plot()

相关问题