【问题标题】:Ellipsoid creation in Python在 Python 中创建椭圆体
【发布时间】:2014-12-04 19:11:11
【问题描述】:

我遇到了一个与绘制椭圆体有关的问题。

我要绘制的椭圆体如下:

x**2/16 + y**2/16 + z**2/16 = 1.

所以我看到了很多与计算和绘制椭圆空隙有关的参考资料,并且在多个问题中提到了笛卡尔到球形或反之亦然的计算。

访问了一个有计算器的网站,但我不知道如何成功执行此计算。此外,我不确定 linspaces 应该设置为什么。已经看到了我在那里的默认库,但由于我以前没有使用这些库的经验,我真的不知道会从中得到什么。

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

fig = plt.figure(figsize=plt.figaspect(1))  # Square figure
ax = fig.add_subplot(111, projection='3d')

multip = (1, 1, 1) 
# Radii corresponding to the coefficients:
rx, ry, rz = 1/np.sqrt(multip)

# Spherical Angles
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)

# Cartesian coordinates

#Lots of uncertainty.
#x = 
#y = 
#z = 

# Plot:
ax.plot_surface(x, y, z,  rstride=4, cstride=4, color='b')

# Axis modifications
max_radius = max(rx, ry, rz)
for axis in 'xyz':
    getattr(ax, 'set_{}lim'.format(axis))((-max_radius, max_radius))

plt.show()

【问题讨论】:

    标签: python math matplotlib coordinate-systems


    【解决方案1】:

    你的椭球不仅仅是一个椭球,它是一个球体。

    请注意,如果您使用下面为 x、y 和 z 编写的替换公式,您将获得一个恒等式。通常,在不同的坐标系(在这种情况下为球形)中绘制这样的旋转表面更容易,而不是尝试求解隐式方程(在大多数绘图程序中最终会出现锯齿状,除非你采取一些对策)。

    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    import numpy as np
    
    phi = np.linspace(0,2*np.pi, 256).reshape(256, 1) # the angle of the projection in the xy-plane
    theta = np.linspace(0, np.pi, 256).reshape(-1, 256) # the angle from the polar axis, ie the polar angle
    radius = 4
    
    # Transformation formulae for a spherical coordinate system.
    x = radius*np.sin(theta)*np.cos(phi)
    y = radius*np.sin(theta)*np.sin(phi)
    z = radius*np.cos(theta)
    
    fig = plt.figure(figsize=plt.figaspect(1))  # Square figure
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_surface(x, y, z, color='b')
    

    【讨论】:

    • 在我的任务中,所有 3 种情况下的半径都是 4。如果我没有x**2/16,而是12x**2,会怎样?会是x = (np.sin(theta)*np.cos(phi))/12吗?
    • @Divine Close,但您忘记了平方根。应该是:x = (np.sin(theta)*np.cos(phi))/np.sqrt(12)。只需确保通过替换这些转换公式,您最终会得到一个身份 (1 == 1)。
    猜你喜欢
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 2019-12-06
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    相关资源
    最近更新 更多