gpt4 book ai didi

python matplotlib.补丁: draw a Circle patch but keep only part of the circle

转载 作者:行者123 更新时间:2023-12-01 09:12:08 31 4
gpt4 key购买 nike

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

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

这是我的代码:

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

之后,运行以下命令:

plt.figure(figsize=(16, 22))
plt.xlim(-600,600)
plt.ylim(-100,1700)
plot_pic()
plt.show()

非常感谢您的帮助。

最佳答案

如果真的只是按照你说的做,你可以将矩形的facecolor设置为白色,将圆形的zorder设置为0 所以它被绘制在后面:

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

enter image description here

关于python matplotlib.补丁: draw a Circle patch but keep only part of the circle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51573606/

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