据我所知,使用
geopandas
没有直接的方法来实现这一点。但是,结合
pyplot
中的散点图可以得到所需的结果。
第一个诀窍是获取饼图绘制的
x,y
坐标。在我的情况下,我使用了
geopandas
的
centroid
工具和x、y方法,例如,x和y坐标由以下公式给出(
i
是geopandas DataFrame中的行索引):
centroids = geo_df.centroid
x_coord = centroids.x[i]
y_coord = centroids.y[i]
下一步是使用pyplot创建
饼图并存储返回的结构,其中包括饼图楔形的列表。
wedges=plt.pie([1,1,1])
第三步是创建一个绘图对象,然后使用
plot API绘制geopandas DataFrame,例如:
fig, ax = plt.subplots(1, 1)
geo_df.boundary.plot(ax=ax,zorder=1,edgecolor='black')
最后,使用scatter在geopandas图上添加饼图。
for
循环是为了添加从
plot.pie([1,1,1])
获取的3个楔形图(如果有更多楔形图,请更改此内容。必须有一种更有效/优雅的方法来完成此操作):
piecolor=['b','g','y']
for j in range(3):
ax.scatter([x_coord],[y_coord],marker=(wedges[0][j].get_path().vertices.tolist()),facecolor=piecolors[j], s=800)
plt.show()
关键是向scatter传递一个非默认标记,即楔形图案。对于
wedges
中的每个元素,首先获取路径,然后获取顶点,最后将其转换为列表并作为标记。
kind
参数(https://geopandas.org/docs/reference/api/geopandas.GeoDataFrame.plot.html?highlight=pie),但对我来说它不起作用。你找到解决办法了吗? - Arraval