【问题标题】:How can I get arc from a 3D circle in Matplotlib?如何从 Matplotlib 中的 3D 圆中获取弧?
【发布时间】:2019-11-15 05:07:53
【问题描述】:

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

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

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 轴相切的圆弧角度?

【问题讨论】:

    标签: python python-2.7 matplotlib kinematics


    【解决方案1】:

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

    要改变角度或弧度,有几种方法。我认为更直接的方法是通过为 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      • 2019-11-14
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多