Python中椭球体的创建
我遇到了一个有关绘制椭球体的问题 我要绘制的椭球体如下所示:Python中椭球体的创建,python,math,matplotlib,coordinate-systems,Python,Math,Matplotlib,Coordinate Systems,我遇到了一个有关绘制椭球体的问题 我要绘制的椭球体如下所示: x**2/16 + y**2/16 + z**2/16 = 1. 所以我看到了很多关于计算和绘制椭圆空洞的参考资料,在多个问题中提到了从笛卡尔坐标到球面的计算,反之亦然 我偶然发现一个网站上有一个计算器,但我不知道如何成功地进行这个计算。我也不确定linspace应该设置为什么。我已经看到了我在那里的默认设置,但是由于我以前没有使用这些库的经验,我真的不知道从中可以得到什么 from mpl_toolkits.mplot3d imp
x**2/16 + y**2/16 + z**2/16 = 1.
所以我看到了很多关于计算和绘制椭圆空洞的参考资料,在多个问题中提到了从笛卡尔坐标到球面的计算,反之亦然
我偶然发现一个网站上有一个计算器,但我不知道如何成功地进行这个计算。我也不确定linspace应该设置为什么。我已经看到了我在那里的默认设置,但是由于我以前没有使用这些库的经验,我真的不知道从中可以得到什么
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()
你的椭球不仅仅是一个椭球,它是一个球体 请注意,如果您使用下面为x、y和z编写的替换公式,您将获得一个标识。通常,在不同的坐标系(本例中为球形)中绘制这样的旋转曲面更容易,而不是尝试求解隐式方程(在大多数绘图程序中,除非采取一些对策,否则隐式方程最终会出现锯齿状)
在我的任务中,3例患者的半径均为4。如果我没有使用
x**2/16
,而是使用12x**2
?它会是x=(np.sin(θ)*np.cos(φ))/12
?@神圣接近,但你忘了平方根。它将是:x=(np.sin(θ)*np.cos(phi))/np.sqrt(12)
。只要确保通过替换这些转换公式,最终得到一个标识(1==1
)。
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')