python - 无法为 GeoDataFrame 点着色
问题描述
我想绘制整个巴黎的位置并按列类型为它们着色。我还想绘制与位置类型相关的图例。我可以创建 GeoDataframe,绘制整个巴黎的位置。但是我无法指定地图上点的颜色。此外,我必须单独创建一个图例,然后将其应用于情节。
如何让绘图点的颜色与图例列相匹配?
代码:
paris = gpd.read_file(shape_filepath)
ax = paris.plot(figsize=(40,10), linewidth=1, edgecolor='white', color='lightgrey')
ax.axis('off')
ax.set_title("Casino Banner Stores Paris", fontdict={"fontsize": "25","fontweight" : "5"})
xl = r"summary.xlsx"
df =pd.read_excel(xl,sheet_name = "paris_stores_and_metro_chart")
df = df.dropna()
#Create Points for Shape file - these are long lat pairs
geometry =[Point(xy) for xy in zip(df["lng"],df["lat"])]
crs ={'init':'espg:4326'}
#Create GeoDataFrame
stores =gpd.GeoDataFrame(df,crs=crs,geometry=geometry)
stores.plot(ax=ax,figsize=(40,10), column=stores['colour'], cmap=None)
legend_elements = [
Line2D([0],[0], markerfacecolor ='#0000ff',marker='o', color='w',label ='casino'),
Line2D([0],[0], markerfacecolor ='#3366ff',marker='o', color='w', label ='fanrpix'),
Line2D([0],[0], markerfacecolor ='#00e6b8',marker='o', color='w',label ='geant'),
Line2D([0],[0], markerfacecolor ='#e6e600',marker='o', color='w', label ='leader price'),
Line2D([0],[0], markerfacecolor ='#e65c00',marker='o', color='w', label ='metro station'),
Line2D([0],[0], markerfacecolor ='#ff00ff',marker='o', color='w', label ='monoprix'),
Line2D([0],[0], markerfacecolor ='#e60000',marker='o', color='w', label ='naturalia')
]
ax.legend(handles=legend_elements, fontsize =20)
解决方案
理想情况下,您必须使用 GeoPandas 0.6.3 或 0.7.0。然后你应该能够在绘图期间将你的colour
列传递给color
关键字,并且 geopandas 应该将分配给每一行的颜色映射到它的几何图形。请参阅以下说明该行为的代码段:
import geopandas as gpd
from shapely.geometry import Point
g = [Point(0, 0), Point(1, 0), Point(1,1), Point(0.5, .5)]
gdf = gpd.GeoDataFrame(geometry=g)
gdf['colour'] = ['#0000ff', 'r', '#0000ff', 'k']
gdf.plot(color=gdf.colour)
在您的情况下,这应该可以解决问题:
stores.plot(ax=ax, figsize=(40,10), color=stores['colour'])
如果没有,那么您要么使用的是旧版本的 geopandas,要么存在错误。在这种情况下,请报告。
推荐阅读
- azure - 用于检查用户在 Azure 订阅中被分配角色(例如所有者)的时间戳的 Powershell 脚本
- php - PHP:如何将 Windows FILETIME 字符串从 LDAP 转换为可读的 DATE?
- function - Perl - 打破数学规则的简单计算器
- javascript - 使用 Jest 模拟函数时出现错误
- node.js - 路由正在发送空响应
- javascript - Nodejs 和 Google 电子表格集成(无法读取未定义的属性“长度”)
- performance - 整数乘法的时间成本是否与 ARM 或 Intel 处理器上的任何二进制运算相同?
- ansible - 使用角色之外的文件目录
- python - 从 Anaconda 提示符运行但不能从 Cmd 或 .exe 运行 Pytorch 可执行文件?
- java - 使用 JDBC TEMPLATE 从 Java 调用 oracle 存储过程时出现问题