python - 用一圈点做一个球体
问题描述
我有一个圆点圆 现在我需要制作一个球体。有人可以帮我吗。我想我必须使用 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()
解决方案
这是你要找的吗?我不建议绘制那么多点,它很重,尝试绘制更少的点(就像在代码注释中一样)......
也尽量不要使用“过滤器”,它是 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()
推荐阅读
- amazon-web-services - 尝试连接到 ec2 时,云端显示“无法满足请求 502 错误”
- html - div 的第二个子元素应保持不变
- haskell - 你可以使用 `fromIntegral` 来解决这种类型的错误吗?
- node.js - 在 Windows 服务器上托管 nodejs 的最佳(安全)方式
- html - 如何在 Web 视图中以表格形式对齐数据,在移动视图中以列表形式对齐数据?
- java - Mockito 只能模拟非私人和非最终课程
- vb6 - 从 DLL res 文件中将希伯来语字符串读入 VB6
- git - 为什么 repo sync 没有按照清单 XML 文件中指定的方式提取正确的提交?
- javascript - 如何让 Prettier 忽略一段代码?
- sql - 从包含查询的列中获取表的名称