Matplotlib:如何在Arc中绘制边缘线-补丁

时间:2018-07-28 18:19:12

标签: python matplotlib

鉴于Arcmatplotlib.patches的旋转椭圆的中心和两个角度,我想绘制从弧的中心到弧的两端的两条线。

这里有一段代码可以做到这一点:

import matplotlib.pyplot as plt
from matplotlib.patches import Arc

fig, ax = plt.subplots(1,1)

r = 2 #Radius
a = 0.2*r #width 
b = 0.5*r #height

#center of the ellipse
x = 0.5
y = 0.5

ax.add_patch(Arc((x, y), a, b, angle = 20,
             theta1=0, theta2=120, 
             edgecolor='b', lw=1.1))
#Now look for the ends of the Arc and manually set the limits
ax.plot([x,0.687],[y,0.567], color='r',lw=1.1)
ax.plot([x,0.248],[y,0.711], color='r',lw=1.1)

plt.show()

这将导致

ellipse

在此处绘制红线,仔细观察弧的末端。但是,由于Arc不允许填充弧以进行优化,因此我想知道是否有一种方法可以针对任何中心和角度自动进行优化。

1 个答案:

答案 0 :(得分:0)

  

根据Wikipedia,其极性形式的椭圆看起来像

     

enter image description here

使用此方法,您可以计算直线的终点。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Arc

fig, ax = plt.subplots(1,1)

r = 2 #Radius
a = 0.2*r #width 
b = 0.5*r #height

#center of the ellipse
x = 0.5; y = 0.5

# angle
alpha = 20

ax.add_patch(Arc((x, y), a, b, angle = alpha,
             theta1=0, theta2=120, 
             edgecolor='b', lw=1.1))

def ellipse(x0,y0,a,b,alpha,phi):
    r = a*b/np.sqrt((b*np.cos(phi))**2 + (a*np.sin(phi))**2)
    return [x0+r*np.cos(phi+alpha), y0+r*np.sin(phi+alpha)]

x1,y1 = ellipse(x, y, a/2., b/2., np.deg2rad(alpha), np.deg2rad(0))
ax.plot([x,x1],[y,y1], color='r',lw=1.1)

x2,y2 = ellipse(x, y, a/2., b/2., np.deg2rad(alpha), np.deg2rad(120))
ax.plot([x,x2],[y,y2], color='r',lw=1.1)

ax.set_aspect("equal")
plt.show()

enter image description here