我正在尝试绘制一张图片,我绘制了一个矩形,然后我想绘制一个弧形元素,但这个元素必须精确,它只是矩形形状外的圆的一部分。因此,我尝试使用弧形补丁来创建相同的东西,但形状不匹配。
因此,我想知道是否可以绘制圆,但只保留矩形之外的部分?更具体地说,我想丢弃/隐藏/去掉下图中的蓝色箭头部分,而保留红色箭头部分,它位于矩形之外,就像一个圆弧形状。有什么方法可以做到这一点吗?
以下是我的代码:
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()
非常感谢你的帮助。
发布于 2018-07-29 01:27:50
如果真的只是按照你说的做,你可以将矩形的facecolor设置为white
,将圆的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
https://stackoverflow.com/questions/51573606
复制相似问题