如何在Matplotlib中从3D圆获得弧?

时间:2019-07-04 12:45:03

标签: python python-2.7 matplotlib kinematics

我正在尝试使用matplotlib从与 Z 轴相切的圆中获取圆弧,如下图所示。

enter image description here

enter image description here

我只想要一个被黄色矩形覆盖的弧。下面是获取圆圈的代码。

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
r = input('Enter the radius: ')
d = 2*r

theta = np.linspace(0, 2 * np.pi, 201)
y = d*np.cos(theta)
z = d*np.sin(theta)

for i in range(1):
    phi = i*np.pi    
    ax.plot(y*np.sin(phi)+d*np.sin(phi),
            y*np.cos(phi)+d*np.cos(phi), z)

ax.plot((0,0),(0,0), (-d,d), '-r', label='z-axis')
ax.set_xlabel('X-Axis')
ax.set_ylabel('Y-Axis')
ax.set_zlabel('Z-Axis')
ax.legend()

plt.show()

如果您能提供以下信息,我将不胜感激,

  1. 如何获得弧线?
  2. 如何更改在 X-Y 平面上与 Z 轴相切的圆弧角度?

1 个答案:

答案 0 :(得分:0)

要在YZ平面中显示圆弧/圆,方程非常简单:

其中y0和z0是圆心,R是半径。

此等式的解决方案是:

其中跨过具有完整的圆圈。

然后,您可以简单地将的域限制为只有弧而不是圆:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

r = 5.
y0 = r # To have the tangent at y=0
z0 = 0.

# Theta varies only between pi/2 and 3pi/2. to have a half-circle
theta = np.linspace(np.pi/2., 3*np.pi/2., 201)

x = np.zeros_like(theta) # x=0
y = r*np.cos(theta) + y0 # y - y0 = r*cos(theta)
z = r*np.sin(theta) + z0 # z - z0 = r*sin(theta)

ax.plot(x, y, z)

ax.plot((0, 0), (0, 0), (-r, r), '-r', label='z-axis')
ax.set_xlabel('X-Axis')
ax.set_ylabel('Y-Axis')
ax.set_zlabel('Z-Axis')
ax.legend()

plt.show()

circle

要更改角度或圆弧,有几种方法。我认为最直接的方法是通过为y和z设置不同的半径来绘制椭圆的弧线(而不是圆):

x = np.zeros_like(theta) # x=0
y = a*np.cos(theta) + y0 # y - y0 = a*cos(theta)
z = b*np.sin(theta) + z0 # z - z0 = b*sin(theta)