matplotlib 如何为椭圆添加轴线

iqjalb3h  于 2023-10-24  发布在  其他
关注(0)|答案(2)|浏览(125)

我有一个简单的代码,

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中搜索参数。elliptical()来绘制那些轴线,但没有找到任何东西。

rpppsulh

rpppsulh1#

可以添加椭圆的长轴和短轴。
在我展示的代码中,我做了主轴,但你需要处理Angular 部分(基于椭圆的点),而我只是将其设置为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()

这就给你留下了这样一个情节:

基本上,总而言之,anotate行让您完成所需的最后几位。
编辑:我可以简化为:

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()

它看起来像这样:

c9qzyr3d

c9qzyr3d2#

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()

产生这样的结果:

希望这对你有帮助。

相关问题