如何在Python中绘制球形线段?

时间:2018-02-20 13:08:33

标签: python mplot3d

如何绘制球形线段,特别是球体"切片"在Python?

我知道如何通过

绘制球体表面
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z,  rstride=4, cstride=4, color='b')
plt.show()

或该代码的某些变体,但我只是绘制了一部分球体,导致如下图像:

https://en.wikipedia.org/wiki/Spherical_segment#/media/File:LaoHaiKugelschicht1.png

如果我通过操纵u和v的定义来改变我上面提到的代码,例如:

u = np.linspace(0, 2 * np.pi, 20)
v = np.linspace(0, np.pi, 20)

球体仍然是整体呈现,但分辨率非常差。 改变linspace范围的起点并不会改变任何东西。

1 个答案:

答案 0 :(得分:0)

我找到了对我有用的东西。你走了:

q = 0.5  # defines upper starting point of the spherical segment
p = 0.8  # defines ending point of the spherical segment as ratio


u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(q, p * np.pi, p * 100)
x = r * np.outer(np.cos(u), np.sin(v)) + x_0
y = r * np.outer(np.sin(u), np.sin(v)) + y_0
z = r * np.outer(np.ones(np.size(u)), np.cos(v)) + z_0
ax.plot_surface(x, y, z, color='b')

enter image description here