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

标签 python matplotlib

我需要画一个有 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/

相关文章:

python - 在后台用大数据集填充QListview

python - 在 python-pptx 中复制自由格式对象

python - 如何使用 opencv 对视频进行 Canny 边缘检测以减少闪烁并显示更粗的线条?

python - Matplotlib:设置标签字体而不设置标签

python - Python matplotlib 图的 Windows 10 任务栏文本

python - 使用python在html文档中查找输入字段的值

python - pickle.load : need more than 2 values to unpack

python - 来自事件坐标的 Matplotlib 日期时间

python - 具有两个分类变量的 Matplotlib 点图

python - Matplotlib迭代设置子图轴大小