python 中的对象和图形

Objects and graphing in python

我是编程新手。我计划创建的程序将用于大学物理研究。 所以我的问题不仅仅是代码问题。为简单起见:我正在进行一个研究项目,该项目涉及创建 3d 单元格(现在我们称它们为矩形)并将点放置在这些矩形的随机顶点处。矩形应该彼此相邻。然后随机点将用于不同的计算。我想知道这是否可以使用 matplotlib。我可以创建一个 cubes/rectangular 棱镜阵列并在它们的顶点上放置随机点吗?另外,Matplotlib 是最好的方法吗?你建议我用另一种方式处理这个问题吗?

你当然可以用 Python 和 Matplotlib 做这样的事情。查看此代码:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Generate a 3D grid
ext = np.arange(0,10,1)
xx, yy, zz = np.meshgrid(ext,ext,ext)
values = np.zeros((10,10,10))

# Put some values on a few vertices
xInd = np.random.randint(0,10,20)
yInd = np.random.randint(0,10,20)
zInd = np.random.randint(0,10,20)

values[[xInd,yInd,zInd]] = np.random.random(20)

# Create a scatter plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
im = ax.scatter(xx,yy,zz,c=values, cmap=plt.cm.spectral_r, edgecolor=None, alpha=0.7)
ax.set_xlabel('x')
ax.set_xlabel('y')
ax.set_xlabel('z')
plt.colorbar(im)
fig.show()

生成下图:

如果您需要使用单位矩形(即实际 width/height/depth),那么您需要稍微复杂一点的数据结构,但同样的想法也可以。