gpt4 book ai didi

python - 有没有一种方法可以绘制一侧带有圆弧的 matplotlib 补丁矩形?

转载 作者:行者123 更新时间:2023-12-04 08:30:10 26 4
gpt4 key购买 nike

我需要画一个有 4 条边的多边形,右边是圆弧。类似这样:

enter image description here

我曾尝试使用 matplotlib 提供的代码来绘制贝塞尔曲线,但没有成功。任何帮助将不胜感激:)

from matplotlib.path import Path
import matplotlib.patches as patches


verts = [
(0., 0.), # P0
(0, 1.), # P1
(1., 1), # P2
(1, 0.), # P3
]

codes = [
Path.MOVETO,
Path.CURVE4,
Path.CURVE4,
Path.CURVE4,
]

path = Path(verts, codes)

fig, ax = plt.subplots()
patch = patches.PathPatch(path, facecolor='none', lw=2)
ax.add_patch(patch)

xs, ys = zip(*verts)
ax.plot(xs, ys, lw=2, color='black', ms=10)
ax.set_xlim(-0.1, 1.1)
ax.set_ylim(-0.1, 1.1)
plt.show()

最佳答案

我不知道这是否是绘制路径对象的正确方法 - 您最好等待对这种方法有经验的人。但在那之前,您可以这样做:

from matplotlib.path import Path
import matplotlib.patches as patches


verts = [
(0, 0), # P0
(0, 1), # P1
(1, 1), # P2
(0.4, 1), #these are the vertices for the
(0.2, 0), #Bezier curve
(0.5, 0), # P3
(0, 0) #and back to P0
]

codes = [
Path.MOVETO, # P0
Path.LINETO, #line to P1
Path.LINETO, #line to P2
Path.CURVE4, #control point one for the Bezier curve
Path.CURVE4, #control point two
Path.CURVE4, #end point for the Bezier curve
Path.LINETO #and back to P0
]

path = Path(verts, codes)

fig, ax = plt.subplots()
patch = patches.PathPatch(path, facecolor='none', lw=2)
ax.add_patch(patch)

#you can add the hull figure for the Bezier curve
#xs, ys = zip(*verts)
#ax.plot(xs, ys, "x--", lw=2, color='black', ms=10)

ax.set_xlim(-0.1, 1.1)
ax.set_ylim(-0.1, 1.1)
plt.show()

示例输出:

enter image description here

matplotlib documentation说对于贝塞尔曲线,你需要(除了你当前的位置)两个控制点和一个终点。因此,您的四点方法可能还不够。

关于python - 有没有一种方法可以绘制一侧带有圆弧的 matplotlib 补丁矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65064836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com