首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >python matplotlib.patches:绘制一个圆形补丁,但只保留部分圆形

python matplotlib.patches:绘制一个圆形补丁,但只保留部分圆形
EN

Stack Overflow用户
提问于 2018-07-29 01:17:38
回答 1查看 1.2K关注 0票数 1

我正在尝试绘制一张图片,我绘制了一个矩形,然后我想绘制一个弧形元素,但这个元素必须精确,它只是矩形形状外的圆的一部分。因此,我尝试使用弧形补丁来创建相同的东西,但形状不匹配。

因此,我想知道是否可以绘制圆,但只保留矩形之外的部分?更具体地说,我想丢弃/隐藏/去掉下图中的蓝色箭头部分,而保留红色箭头部分,它位于矩形之外,就像一个圆弧形状。有什么方法可以做到这一点吗?

以下是我的代码:

代码语言:javascript
复制
from matplotlib.patches import Circle, Rectangle, Arc, Ellipse

def plot_pic(ax=None, color='black', lw=2, scale = 15):
    # get the current ax if ax is None
    if ax is None:
       ax = plt.gca()


    # Plot the rectangle
    rec =  Rectangle((-(7.32 * scale / 2+ 5.5 * scale +11 * scale),0), width = (5.5 * scale * 2 + 11 * scale * 2 + 7.32 * scale), height = 16.5 * scale, linewidth = lw, color = color, fill = False)

    testCircle = Circle((0, 11 * scale), radius = 9.15 * scale, color = color, lw = lw, fill = False)


    # List of elements to be plotted
    pic_elements = [rec, testCircle]


    # Add the elements onto the axes
    for element in pic_elements:
        ax.add_patch(element)

    return ax

在此之后,运行以下命令:

代码语言:javascript
复制
plt.figure(figsize=(16, 22))
plt.xlim(-600,600)
plt.ylim(-100,1700)
plot_pic()
plt.show()

非常感谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-29 01:27:50

如果真的只是按照你说的做,你可以将矩形的facecolor设置为white,将圆的zorder设置为0,这样它就会绘制在后面:

代码语言:javascript
复制
def plot_pic(ax=None, color='black', lw=2, scale = 15):
    # get the current ax if ax is None
    if ax is None:
       ax = plt.gca()


    # Plot the rectangle
    rec =  Rectangle((-(7.32 * scale / 2+ 5.5 * scale +11 * scale),0), width = (5.5 * scale * 2 + 11 * scale * 2 + 7.32 * scale), height = 16.5 * scale, linewidth = lw, color = color, fc='white')

    testCircle = Circle((0, 11 * scale), radius = 9.15 * scale, color = color, lw = lw, fill = False, zorder=0)


    # List of elements to be plotted
    pic_elements = [rec, testCircle]


    # Add the elements onto the axes
    for element in pic_elements:
        ax.add_patch(element)

    return ax

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51573606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档