如何向椭圆添加轴线

问题描述 投票:0回答:2

我有这个生成椭圆的简单代码

import matplotlib.patches as patches
import matplotlib.pyplot as plt

fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})

ellipse = patches.Ellipse((0, 0), 4, 2, angle=45, fill=False)
ax.add_artist(ellipse)

ax.set_xlim(-2.2, 2.2)
ax.set_ylim(-2.2, 2.2)

plt.show()

这是当前输出: ellipse

我需要添加椭圆轴,所以它看起来像这样: ellipse_output

有办法做到这一点吗? 我需要一种通用的方法来使用更复杂的椭圆,谢谢。

我尝试在 patch.Ellipse() 中搜索参数来绘制这些轴线,但没有找到任何东西。

python matplotlib ellipse
2个回答
1
投票

您可以添加椭圆的长轴和短轴。

在我展示的代码中,我制作了主轴,但是您需要处理角度部分(基于椭圆的点),而我只是将其设置为 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()

看起来像这样:


0
投票
from math import sin, cos, radians
import matplotlib.patches as patches
import matplotlib.pyplot as plt

fig, ax = plt.subplots(subplot_kw={'aspect': 'equal'})

##############
ellipse_size = (4,2)
ellipse_rotation = 45
ellipse_position = (0,0)

ellipse = patches.Ellipse(ellipse_position, ellipse_size[0], ellipse_size[1], angle=ellipse_rotation, fill=False)
ax.add_artist(ellipse)

ax.set_xlim(-2.2, 2.2)
ax.set_ylim(-2.2, 2.2)

# math for the start and end axis point positions
ax1_points = [
    (ellipse_position[0]+ellipse_size[0]/2*cos(radians(ellipse_rotation)), 
    ellipse_position[1]+ellipse_size[0]/2*sin(radians(ellipse_rotation))),
    (ellipse_position[0]+ellipse_size[0]/2*cos(radians(ellipse_rotation + 180)), 
    ellipse_position[1]+ellipse_size[0]/2*sin(radians(ellipse_rotation + 180)))
    ]
ax2_points = [
    (ellipse_position[0]+ellipse_size[1]/2*cos(radians(ellipse_rotation+90)), 
    ellipse_position[1]+ellipse_size[1]/2*sin(radians(ellipse_rotation+90))),
    (ellipse_position[0]+ellipse_size[1]/2*cos(radians(ellipse_rotation + 270)), 
    ellipse_position[1]+ellipse_size[1]/2*sin(radians(ellipse_rotation + 270)))]

# ax1 and ax2 contains the start and the end point of the axis ([x,y] format)

# drawing the arrows
arrowprops=dict(arrowstyle="<->", color="red")
ax.annotate("", xy=ax1_points[0], xytext=ax1_points[1], arrowprops=arrowprops)
ax.annotate("", xy=ax2_points[0], xytext=ax2_points[1], arrowprops=arrowprops)
plt.show()

产生这个:

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.