如何解决matplotlib.patches.Arc并使用clip_path参数
我想在matplotlib.patches.Arc中使用clip_path参数,但不会成功。 接下来仅是一个示例,在这里我不想通过使用clip_path参数看到完整的橙色弧,而仅看到y轴和红色圆圈之间的部分橙色弧,但是却不了解如何定义clip_path参数。谢谢。
import math as m
import matplotlib.pyplot as plt
import matplotlib.patches as pat
plt.figure(figsize=(10,10),dpi=300)
ag=10
plt.axis([-ag,ag,-ag,ag])
plt.grid(True)
circle1 = plt.Circle((0,2.5),7,color='r',fill=False)
plt.gcf().gca().add_artist(circle1)
myarc=pat.Arc((0,0),25,18,angle=0,theta1=0,theta2=355,color="orange")
plt.gcf().gca().add_artist(myarc)
plt.savefig("myarc.png")
plt.show()
这就是我得到的:
再说一遍:有了theta1和theta2角度的下一个修改,我得到了所需的东西,但是为此,首先需要确定两个交点。我的目的是避免这些计算,而只是绘制一个椭圆并定义两个剪切路径(红色圆圈和y轴)。
myarc = pat.Arc((0,0),25,18,angle = 0,theta1 = 110,theta2 = 152,color =“ orange”)
解决方法
要按圆弧修剪圆弧,可以使用myarc.set_clip_path(circle1)
。重要的是,弧和圆都必须预先添加到绘图中(ax.add_artist()
)。请注意,会自动进行轴的边界剪切。
要创建更复杂的剪辑,shapely可能会更方便。
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
fig,ax = plt.subplots(figsize=(10,6))
circle1 = plt.Circle((0,2.5),7,color='r',fill=False)
ax.add_artist(circle1)
myarc = mpatches.Arc((0,0),25,18,angle=0,theta1=0,theta2=355,color="orange",lw=5)
ax.add_artist(myarc)
myarc.set_clip_path(circle1)
ag = 10
ax.set_xlim(-ag,ag)
ax.set_ylim(-ag,ag)
plt.grid(True)
ax.set_aspect('equal') # set the aspect ratio so circles look like circles
plt.show()
,
通过使用答案的步骤(1),我不需要计算所有交点就得到了所需的结果。步骤:
-
曲线的定义和绘图
-
使用clip_path选项定义剪切区域(例如,通过数学函数结果将一维数组连接成圆形或修整区域)
-
使用clip_path摆脱不必要的曲线部分
import copy def func_a(list1): list1.append("dada") list1[0]['a']=333 return list1 def main(): list_1 = [{'a':1},{'b':2},{'c':3}] list_2 = func_a(copy.deepcopy(list_1)) print(list_1) print("\n") print(list_2) main() [{'a': 1},{'b': 2},{'c': 3}] [{'a': 333},{'c': 3},'dada']
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。