如何在不创建另一个数据框的情况下绘制 Geo panda 框架的质心

问题描述 投票:0回答:1

地理熊猫新手。

我有一个 geopanda 框架,其中包含县名和几何形状。

尝试在地图上绘制这些县,其中边界的面积和名称标注在每个县的质心处。

我通过为每个需要绘制的信息(如面积或质心坐标)创建一个 Geo panda 框架,以一种非常黑客的方式工作。 有没有更好的绘图方法


fig, ax = plt.subplots(figsize=(12, 12))

# Plot counties
counties.plot(ax=ax , color='steelblue', edgecolor='#6a6a6a', linewidth=2 , alpha =0.5 , kind="geo")

# Draw centroids with red circle 

## Method 1: works but need a new gdf created for centroids 
county_centroids.plot(ax=ax , color='red', marker='o', markersize=5 )


## Method 2: NOT WORKING.  Attempt to calculate centroid and plot on the fly  
counties.plot(ax=ax,  color ="red",  marker="o", markersize=6 , x=counties.geometry.x , y =counties.geometry.y , kind = "scatter")

我想知道是否有某种方法可以像马赛克一样不断添加一层又一层,并在最后绘制它。

谢谢

geopandas
1个回答
0
投票

此演示代码展示了如何在同一绘图轴 (ax1) 上绘制多组特征:

import geopandas as gpd

# Create a geo-dataframe of countries of the world using cloud data
# This is the only geo-dataframe that is created
gpd_lowres = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

# Plot polygons of 7 selected countries
# The selected countries are NOT retained as a geo-dataframe, 
#  but the axis `ax1`, which is necessary for use in next plots
ax1 = gpd_lowres.head(7).plot(figsize=[10,7], color="lightgray")

# Plot buffered-polygons of the centroids
gpd_lowres.head(7).geometry.representative_point().buffer(distance=8).plot(ax=ax1, color="magenta", alpha=0.35)

# Plot the centroids
gpd_lowres.head(7).geometry.representative_point().plot(ax=ax1, color="blue",  marker="o", markersize=40 )

# Plot + marks at the centroids and dashed lines connecting the centroids
ax1.plot(gpd_lowres.head(7).geometry.representative_point().x,
         gpd_lowres.head(7).geometry.representative_point().y, 'r+--') 

# Plot names of the countries
for (x,y,t) in zip(gpd_lowres.head(7).geometry.representative_point().x.to_list(),
         gpd_lowres.head(7).geometry.representative_point().y.to_list(),
         gpd_lowres.head(7).name.to_list()):
    ax1.text(x, y, '   '+t, fontsize=6, fontweight='ultralight', color="k")

outputplot

© www.soinside.com 2019 - 2024. All rights reserved.