创建立方体阵列
问题描述:
以下程序将用于大学物理研究。所以我正在研究一个涉及以矩形形式创建单元格的研究项目。现在我正在使用3d立方格。我的目标是让更多这些彼此相邻。创建立方体阵列
这里是我当前的代码(你们帮助过): 现在它只能制作一个立方体,可以在立方体网格的顶点上放置随机点,并且它可以计算点之间的距离。我将如何让matplotlib生成更多的这些?另外,我需要能够在多个立方体上放置点,并且我应该能够计算从立方体A中的点到立方体B中的点的距离。我是否可以通过仅在同一个matplotlib中生成多个立方体做一段时间循环?另外,它看起来像使用numpy数组呢? 我感觉好像numpy数组很容易创建,但我不能完全包裹它。 代码我至今:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import proj3d
parameter = np.arange(0,11,1)
xx, yy, zz = np.meshgrid(parameter, parameter, parameter)
valuesrange = np.zeros((11, 11, 11))
valuesrange2 = np.zeros((11, 11, 11))
count = 0
while (count < 2):
xint = np.random.randint(0,2)
yint = np.random.randint(0,2)
zint = np.random.randint(0,2)
if xint > 0:
xint = np.random.randint(10,11, 22)
else:
xint = np.random.randint(0,1, 22)
if yint >0:
yint = np.random.randint(10,11, 22)
else:
yint = np.random.randint(0,1, 22)
if zint > 0:
zint = np.random.randint(10,11, 22)
else:
zint = np.random.randint(0,1, 22)
count = count + 1
print(xint, yint, zint)
xint2 = np.random.randint(0,2)
yint2 = np.random.randint(0,2)
zint2 = np.random.randint(0,2)
if xint2 > 0:
xint2 = np.random.randint(10,11, 22)
else:
xint2 = np.random.randint(0,1, 22)
if yint2 >0:
yint2 = np.random.randint(10,11, 22)
else:
yint2 = np.random.randint(0,1, 22)
if zint2 > 0:
zint2 = np.random.randint(10,11, 22)
else:
zint2 = np.random.randint(0,1, 22)
print (count)
print(xint2, yint2, zint2)
distance = ((xint2-xint)**2 + (yint2 - yint)**2 + (zint2 - zint)**2)**.5
print ('distance:')
print (distance)
#xint = np.random.randint(0, 11, 22)
#yint = np.random.randint(0, 11, 22)
#zint = np.random.randint(0, 11, 22)
#distance formula = ((x2-x1)**2 + (y2 - y1)**2 + (z2 - z1)**2)**.5
valuesrange[[xint, yint, zint]]= np.random.random(22)
valuesrange[[xint, yint, zint]]= np.random.random(22)
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
im = ax.scatter(xx, yy, zz, c = valuesrange, cmap=plt.cm.spectral_r,
edgecolor = 'none', alpha = .7)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.colorbar(im)
fig.show()
#plt.show()
答
我有认识的大部分代码的目的,严重的问题。我可以告诉你的是,你当然可以将多维数据集创建放入一个函数中,并可能使用不同的参数多次调用该函数,以获得多个散点图。
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
def make_cube(ax, n=10, offset=[0,0,0]):
parameter = np.arange(0,n,1)
xx, yy, zz = np.meshgrid(parameter, parameter, parameter)
valuesrange = np.zeros((n,n,n))
valuesrange = np.random.rand(n,n,n)
x = xx+offset[0]; y=yy+offset[1]; z=zz+offset[2]
sc = ax.scatter(x, y, z, c = valuesrange, cmap=plt.cm.spectral_r, vmin=0, vmax=1,
edgecolor = 'none', alpha = .7)
return sc
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
sc1 = make_cube(ax,n=6)
sc2 = make_cube(ax,n=4, offset=[8,7,4])
# or use a loop:
#for i in range(4):
# sc1 = make_cube(ax,n=i)
plt.colorbar(sc1)
plt.show()
对不起,我没有说清楚。我认为你可能没有得到的部分是if/else语句。这些语句在立方体的顶点上产生随机点。另外,你的立方体网格中两种不同分布点的用途是什么?那些应该代表Unitcells?谢谢。 – Astupidhippo
我不知道他们代表什么。您要求提供一种方法来制作多个立方体,并且此答案提供了这种方法。我让这两个立方体不同,但这只是为了展示如何使用参数,因为我不知道(而且我也不需要知道)所有这些的目的。 – ImportanceOfBeingErnest
好的。我想我现在明白这一点(尤其是参数)。谢谢您的帮助。 – Astupidhippo