You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何增加matplotlib路径顶点数量,提升路径拟合精度?

增加Matplotlib路径顶点数量的两种解决办法

方法一:手动生成高分辨率圆弧顶点

mpat.Arc内部靠少量贝塞尔曲线绘制圆弧,默认get_path()返回的顶点数量有限。你可以直接计算更多角度对应的坐标,精准控制顶点密度:

import numpy as np
from matplotlib import pyplot as plt
import matplotlib.patches as mpat

fig, ax = plt.subplots()
ax.set(xlim=(-6, 6), ylim=(-6, 6))

# 添加圆弧到画布
circle = mpat.Arc((0, 0), 10, 10, theta1=20, theta2=220, color='green')
ax.add_patch(circle)

# 生成高分辨率顶点:调整num_points控制数量
num_points = 100
theta = np.linspace(np.deg2rad(20), np.deg2rad(220), num_points)
center = (0, 0)
width, height = 10, 10
# 计算每个角度对应的坐标
x = center[0] + (width/2)*np.cos(theta)
y = center[1] + (height/2)*np.sin(theta)
path = np.column_stack((x, y))

# 绘制顶点
plt.scatter(x=path[:, 0], y=path[:, 1], color='red', s=2)

plt.show()

方法二:对现有路径插值细分

如果不想手动计算,也可以用Path的interpolated()方法对现有路径进行细分,通过steps参数控制细分程度:

import numpy as np
from matplotlib import pyplot as plt
import matplotlib.patches as mpat
from matplotlib.path import Path

fig, ax = plt.subplots()
ax.set(xlim=(-6, 6), ylim=(-6, 6))

# 添加圆弧到画布
circle = mpat.Arc((0, 0), 10, 10, theta1=20, theta2=220, color='green')
ax.add_patch(circle)

# 获取变换后的路径并插值细分
original_path = circle.get_transform().transform_path(circle.get_path())
# steps值越大,顶点越密集
interpolated_path = original_path.interpolated(steps=10)
path = interpolated_path.vertices[:-1]  # 移除最后一个重复的起点

# 绘制顶点
plt.scatter(x=path[:, 0], y=path[:, 1], color='red', s=2)

plt.show()

补充说明

  • 方法一适合圆弧、椭圆弧这类规则形状,能完全掌控顶点数量;
  • 方法二更通用,适用于任意复杂路径,steps表示每个路径段的细分步数,数值越大顶点密度越高。

内容的提问来源于stack exchange,提问作者Adrials

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 04:50:24