如何在 Python 中绘制球形段?

How to plot a spherical segment in Python?

如何在 Python 中绘制球形段,特别是球体 "slice"?

我知道如何通过

绘制球面
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 范围的起点似乎没有任何改变。

我找到了适合我的方法。给你:

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')