python颜色填充代码_python-用图案而不是颜色Geopandas填充多边形

最近提交的geopandas允许将剖面线参数传递给GeoDataFrame上的plot方法,因此,如果您是从最新源安装的,并且具有名为gdf的GeoDataFrame,则可以执行以下操作:

gdf.plot(facecolor="#DDDDDD", hatch="//")

然后,您可以根据匹配某个属性来绘制所需的任何子集.从您的示例中,我对您的数据的结构有点不清楚,但是类似:

gdf[gdf["key"] == value1].plot(facecolor="#DDDDDD", hatch="//")

gdf[gdf["key"] == value2].plot(facecolor="#DDDDDD", hatch="o")

等等.

在pip或conda上可用的最新的Geopandas没有此功能.您仍然可以将hatch关键字传递给matplotlib.patches.Polygon(这似乎是您在示例中使用的样式)来控制样式,在上面的示例中,样式是:

poly = Polygon(shape, facecolor="#DDDDDD", alpha=alpha, linewidth=0.01, hatch="\")

要添加图例,可以保留一系列绘制的多边形和关联的标签,并将其传递给图例.因此,在上面的循环中,类似:

polys.append(poly)

labels.append(search_str)

然后,在循环之外:

ax.legend(polys, labels)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是用 Python 实现 X-扫描填充算法并使用 Matplotlib 库进行可视化的代码: ```python import matplotlib.pyplot as plt # 定义填充颜色 fill_color = (0.8, 0.8, 0.8) # 定义边框颜色 edge_color = (0.2, 0.2, 0.2) # 定义扫描线颜色 scanline_color = (0.4, 0.4, 0.4) # 定义图形大小 fig, ax = plt.subplots(figsize=(6, 6)) # 定义多边形顶点 vertices = [(1, 1), (3, 1), (3, 3), (2, 3), (2, 2), (1, 2)] # 绘制多边形边框 ax.plot([v[0] for v in vertices + [vertices[0]]], [v[1] for v in vertices + [vertices[0]]], color=edge_color) # 定义扫描线起点 y_start = 1 # 定义扫描线终点 y_end = 3 # 遍历扫描线 for y in range(y_start, y_end + 1): # 记录交点 intersections = [] # 遍历多边形边界 for i in range(len(vertices)): # 获取当前边和下一个边的顶点 current_vertex = vertices[i] next_vertex = vertices[(i + 1) % len(vertices)] # 判断当前边是否与扫描线相交 if (current_vertex[1] <= y and next_vertex[1] > y) or \ (current_vertex[1] > y and next_vertex[1] <= y): # 计算交点的 x 坐标 x = (y - current_vertex[1]) * \ (next_vertex[0] - current_vertex[0]) / \ (next_vertex[1] - current_vertex[1]) + \ current_vertex[0] # 添加交点 intersections.append(x) # 对交点排序 intersections.sort() # 根据交点坐标填充区域 for i in range(0, len(intersections), 2): ax.fill_betweenx([y, y+1], intersections[i], intersections[i+1], color=fill_color) # 显示扫描线 ax.axhline(y=y_start, color=scanline_color) ax.axhline(y=y_end, color=scanline_color) # 设置坐标轴范围 ax.set_xlim(0, 4) ax.set_ylim(0, 4) # 显示图形 plt.show() ``` 这段代码会绘制一个多边形,并使用 X-扫描填充算法对其进行填充。你可以根据需要修改填充颜色、边框颜色、扫描线颜色等配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值