两种利用matplotlib绘制无填充的多边形的方法:ax.fill()和Polygon

本文介绍了如何使用matplotlib库中的ax.fill()和Polygon类绘制无填充的多边形,包括直接调用ax.fill()函数和使用Polygon对象以及PatchCollection进行批量绘制,通过实例展示了如何设置边色和线条粗细。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两种利用matplotlib绘制无填充的多边形的方法:ax.fill()和Polygon

下面我们将使用np.rand随机生成5个多边形的顶点,使用不同的方法绘制多边形。

ax.fill()绘制多边形

函数原型为:

Axes.fill(*args, data=None, **kwargs)
args参数指的是按x, y, [color]给出的x坐标列表、y坐标列表和多边形的颜色。
data参数是带标记的多边形参数,一般用不上
kwargs是Polygon类型的属性,一般也用不上

ax.fill函数的调用方式为:

# x,y是元素数量相同的list
ax.fill(x, y)                    # a polygon with default color
ax.fill(x, y, "b")               # a blue polygon
# x2,y2也是元素数量相同的list
ax.fill(x, y, x2, y2)            # two polygons
ax.fill(x, y, "b", x2, y2, "r")  # a blue and a red polygon

代码示例如下:

import numpy as np
import matplotlib.pyplot as plt

N=5# N个顶点
points1 = np.random.rand(N, 2)# 第一个多边形的顶点集
points2 = np.random.rand(N, 2)# 第二个多边形的顶点集
points3 = np.random.rand(N, 2)# 第三个多边形的顶点集
points4 = np.random.rand(N, 2)# 第四个多边形的顶点集
fig, ax1 = plt.subplots(1,1)
# 绘制
ax1.fill(points1[:,0],points1[:,1], facecolor='none', edgecolor='orangered', linewidth=2)
ax1.fill(points2[:,0],points2[:,1], facecolor='none', edgecolor='blue', linewidth=1)
ax1.fill(points3[:,0],points3[:,1], facecolor='none', edgecolor='black', linewidth=1)
ax1.fill(points4[:,0],points4[:,1], facecolor='none', edgecolor='green', linewidth=1)
# 显示
p
如何修改以下程序使render能返回一个合理值: def render(self): """ 可视化环境: 1. 未覆盖区域用灰色 2. 已覆盖区域用白色 3. goal_points 用实心三角形表示 4. agents 用实心圆点表示 5. 每个 agent 及其 goal_points 用相同颜色 6. 用虚线绘制 agent 的轨迹 """ if self.fig is None or self.ax is None: plt.ion() self.fig, self.ax = plt.subplots(figsize=(6, 6)) self.ax.clear() self.ax.set_title("Exploration and Coverage") self.ax.set_xlim(0, self.grid_size[0]) self.ax.set_ylim(0, self.grid_size[1]) self.ax.set_aspect("equal") # 颜色映射 colors = list(mcolors.TABLEAU_COLORS.values()) # 绘制网格覆盖状态 grid_map = np.ones((self.grid_size[1], self.grid_size[0], 3)) # 初始化白色背景 for (x, y) in np.ndindex(self.grid_size[0], self.grid_size[1]): if (x, y) not in self.global_coverage: grid_map[y, x] = [0.5, 0.5, 0.5] # 未覆盖区域灰色 self.ax.imshow(grid_map, origin='lower', extent=[0, self.grid_size[0], 0, self.grid_size[1]]) for i, agent in enumerate(self.agents): color = colors[i % len(colors)] # 分配颜色 # # 轨迹(用虚线) # positions = np.array(agent.history) if hasattr(agent, "history") else np.array([agent.position]) # if len(positions) > 1: # self.ax.plot(positions[:, 0], positions[:, 1], linestyle="dashed", color=color, alpha=0.7) # 覆盖园 circle = plt.Circle( (agent.position[0], agent.position[1]), radius=agent.coverage_radius, fill=False, edgecolor=color, alpha=0.3 ) self.ax.add_patch(circle) # agent 位置(实心圆点) self.ax.scatter(agent.position[0], agent.position[1], color=color, marker='o', s=50, label=f'Agent-{i}') # goal_points(实心三角形) self.ax.scatter(agent.goal_pos[0], agent.goal_pos[1], color=color, marker='^', s=70, label=f'Goal-{i}')
最新发布
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值