首页 > 解决方案 > 用一圈点做一个球体

问题描述

我有一个圆点 现在我需要制作一个球体。有人可以帮我吗。我想我必须使用 x 2 + y 2 + z 2 <= R 2 并使用 Axes3D 模块。

import numpy
import matplotlib.pyplot as plt


X = list(range(1, 101))
Y = list(range(1, 101))


x = numpy.array(X)
y = numpy.array(Y)

xgrid, ygrid = numpy.meshgrid(x, y)

plt.style.use('seaborn')
fig, ax = plt.subplots()

filter = (xgrid-50)**2 + (ygrid-50)**2 <= 25**2
ax.scatter(xgrid[filter], ygrid[filter], s= 1, color='green')

ax.set_title('сетка из точек 100х100',
            fontfamily = 'monospace',
            fontstyle = 'normal',
            fontweight = 'bold',
            fontsize = 10)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)

ax.tick_params(axis='both', which='major', labelsize=14)

ax.axis([0, 101, 0, 101])

plt.show()

标签: pythonnumpymatplotlibgeometry

解决方案


这是你要找的吗?我不建议绘制那么多点,它很重,尝试绘制更少的点(就像在代码注释中一样)......

也尽量不要使用“过滤器”,它是 Python 中的保留关键字

import numpy as np
import matplotlib.pyplot as plt

# define sphere parameters
cX, cY, cZ = 50, 50, 50
radius = 25

x = np.array(range(0, 101))
y = np.array(range(0, 101))
z = np.array(range(0, 101))

# try that instead, it is less heavy
# x = np.array(range(0, 101, 5))
# y = np.array(range(0, 101, 5))
# z = np.array(range(0, 101, 5))

xgrid, ygrid, zgrid = np.meshgrid(x, y, z)

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

check = (xgrid - cX)**2 + (ygrid - cY)**2 + (zgrid - cZ)**2 <= radius**2
ax.scatter(xgrid[check], ygrid[check], zgrid[check], color='green')

ax.set_title('сетка из точек 100х100',
            fontfamily = 'monospace',
            fontstyle = 'normal',
            fontweight = 'bold',
            fontsize = 10)
ax.set_xlabel("Value", fontsize=14)
ax.set_ylabel("Square of Value", fontsize=14)

ax.set_xlim3d(0, 101)
ax.set_ylim3d(0, 101)
ax.set_zlim3d(0, 101)

plt.show()

推荐阅读