您可以添加椭圆的长轴和短轴。
在我展示的代码中,我做了长轴,但你需要处理角度部分(基于椭圆的点),而我只是将它设置为 45 度以发布快速答案。
这样的结果将给出完整的解决方案。
所以,我做这样的事情:
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
#################################
# you need to figure this bit out
#################################
ellipse = patches.Ellipse((0, 0), 4, 2, angle=45, fill=False)
ax.add_artist(ellipse)
ellipse.set_clip_box(ax.bbox)
ellipse.set_alpha(0.1)
ax.annotate("",
xy=(ellipse.center[0], ellipse.center[1] - ellipse.height / 2),
xytext=(ellipse.center[0], ellipse.center[1] + ellipse.height / 2),
arrowprops=dict(arrowstyle="<->", color="black"))
ax.annotate("",
xy=(ellipse.center[0] - ellipse.width / 2, ellipse.center[1]),
xytext=(ellipse.center[0] + ellipse.width / 2, ellipse.center[1]),
arrowprops=dict(arrowstyle="<->", color="black"))
ax.annotate("",
xy=(ellipse.center[0] - ellipse.width / 2 * np.cos(np.deg2rad(ellipse.angle)),
ellipse.center[1] - ellipse.height / 2 * np.sin(np.deg2rad(ellipse.angle))),
xytext=(ellipse.center[0] + ellipse.width / 2 * np.cos(np.deg2rad(ellipse.angle)),
ellipse.center[1] + ellipse.height / 2 * np.sin(np.deg2rad(ellipse.angle))),
arrowprops=dict(arrowstyle="<->", color="black"))
ax.set_xlim(-2.2, 2.2)
ax.set_ylim(-2.2, 2.2)
plt.show()
这给你留下了这样的情节:
基本上,总而言之,注释行可让您完成所需的最后几步。
编辑:
我能够减少到这个:
import matplotlib.patches as patches
import matplotlib.pyplot as plt
fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})
# patches.Ellipse(center, width, height, angle)
ellipse = patches.Ellipse((0, 0), 4, 2, angle=45, fill=False)
ax.add_artist(ellipse)
ellipse.set_clip_box(ax.bbox)
ax.annotate("",
xy=(ellipse.center[0] - ellipse.width+2 ,
ellipse.center[1] - ellipse.height ),
xytext=(ellipse.center[0] + ellipse.width-1,
ellipse.center[1] + ellipse.height+1),
arrowprops=dict(arrowstyle="<->", color="red"))
ax.set_xlim(-2.2, 2.2;)
ax.set_ylim(-2.2, 2.2)
plt.show()
看起来像这样: