用Python创建椭圆体 [英] Ellipsoid creation in Python
问题描述
我遇到了一个有关椭圆体绘制的问题.
I have ran into a problem relating to the drawing of the Ellipsoid.
我要绘制的椭圆体如下:
The ellipsoid that I am drawing to draw is the following:
x**2/16 + y**2/16 + z**2/16 = 1.
因此,我看到了许多与椭圆形空隙的计算和绘制有关的参考资料,并且在多个问题中提到了笛卡尔到球面的计算,反之亦然.
So I saw a lot of references relating to calculating and plotting of an Ellipse void and in multiple questions a cartesian to spherical or vice versa calculation was mentioned.
闯入一个有计算器的网站,但我不知道如何成功执行此计算.另外,我不确定应将linspace设置为什么.已经看到了我那里的默认库,但是由于我以前没有这些库的经验,所以我真的不知道从中可以得到什么.
Ran into a website that had a calculator for it, but I had no idea on how to successfully perform this calculation. Also I am not sure as to what the linspaces should be set to. Have seen the ones that I have there as defaults, but as I got no previous experience with these libraries, I really don't know what to expect from it.
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()
推荐答案
您的椭球不仅仅是一个椭球,它是一个球体.
Your ellipsoid is not just an ellipsoid, it's a sphere.
请注意,如果您使用下面编写的x,y和z替代公式,您将获得一个标识.通常,在不同的坐标系(在这种情况下为球形)中绘制这样的旋转表面比在不尝试求解隐式方程式(除非采取某些对策,否则在大多数绘图程序中最终会产生锯齿)要容易得多. /p>
Notice that if you use the substitution formulas written below for x, y and z, you'll get an identity. It is in general easier to plot such a surface of revolution in a different coordinate system (spherical in this case), rather than attempting to solve an implicit equation (which in most plotting programs ends up jagged, unless you take some countermeasures).
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')
这篇关于用Python创建椭圆体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!